create array in cmex

1 view (last 30 days)
khairul ismail
khairul ismail on 18 Jul 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

Answers (1)

Jan
Jan on 18 Jul 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!

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!