Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ 2D growing array like MATLAB

Tags:

c++

arrays

matlab

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.

like image 677
matiasfha Avatar asked Nov 05 '22 01:11

matiasfha


2 Answers

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

like image 192
Chris Avatar answered Nov 09 '22 05:11

Chris


If you're doing image processing, you might want to use the matrix and array types from opencv.

like image 25
SiggyF Avatar answered Nov 09 '22 06:11

SiggyF