I have read some posts about dynamic growing arrays in C, but I can't see how to create a 2D growing array (like in MATLAB).
I have a function to construct an array for some image processing, but I don't know what will be the size of this array (cols and rows). How can I create this?
I read something about malloc and realloc. These functions are portable or useful for this problem.
EDIT: SOLVED, using the Armadillo library, a C++ linear algebra library.
Simplest is with pointers
int nrows = 10;
int ncols = 5;
double* matrix = new double[mrows*ncols];
And then you can access it as if it's a 2D array like.
So if you want matrix[row][col]
, you'd do
int offset = row*ncols+col;
double value = matrix[offset];
Also, if you want the comfort of Matlab like matrixes in C++, look into Armadillo
If you're doing image processing, you might want to use the matrix and array types from opencv.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With