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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by