create array in cmex

4 次查看(过去 30 天)
khairul ismail
khairul ismail 2013-7-18
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

回答(1 个)

Jan
Jan 2013-7-18
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!

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by