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