create array in cmex

1 visualización (últimos 30 días)
khairul ismail
khairul ismail el 18 de Jul. de 2013
in c++ source code, i created the code below to make 2d array: // create empty squares for(int j = 0; j < JDIM; j++) { for(int i = 0; i < IDIM; i++) { squares[i][j] = 0; } }
in mexFunction inside cmex file, i replaced with mxCreateNumericArray() to create above array and i as i understand this function will populate all the elements with 0 initially.
my question is how can i make certain element in the 2d array to be some value. let say in c++ i can make such this code: if true % squares[2][3] = 1; end

Respuestas (1)

Jan
Jan el 18 de Jul. de 2013
mxArray *A;
mwSize JDim = 4, IDim = 5;
double *squares;
A = mxCreateNumericArray(IDim, JDim, mxDOUBLE_CLASS, mxREAL);
squares = mxGetPr(A);
Now squares is a pointer to the data of the array. It can be filled using linear indexing:
i = 2; % 1-based indexing!
j = 3;
squares[i - 1 + (j - 1) * IDim] = 2; % 0-based indexing!

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by