我想索引編寫MATLAB的外部C++函數使用mex操縱矩陣,我不能夠使用多維索引。有提供的例子here,但我還沒有找到如何解決我下面描述的問題。 我有一個樣品基質:當我改變graph_list的定義如何循環遍歷mex的C++函數中的矩陣元素?
>> mexTryAlex(mat)
5 rows
2 cols
1
2
3
4
5
10
20
30
40
50
:
#include <mex.h>
#include <iostream>
using namespace std;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//1.get pointer to input graph_list and allocate it
double *graph_list = mxGetPr(prhs[0]);
mwSize mrows = mxGetM(prhs[0]);
mwSize ncols = mxGetN(prhs[0]);
cout<< mrows<<" rows\n";
cout<< ncols<<" cols\n";
int mm, nn;
for (nn=0;nn<ncols;nn++) {
for (mm=0;mm<mrows;mm++){
cout << graph_list[nn*(mrows) +mm] <<"\n";
}
}
}
這產生:
>> mat
mat =
1 10
2 20
3 30
4 40
5 50
目前我通過其工作的矩陣使用線性指數並嘗試2D索引到graph_list有一個編譯錯誤mex
:
double **graph_list = mxGetPr(prhs[0]);
cout << graph_list[nn][mm];
編輯:這裏是接收的錯誤消息
>> mex mexTryAlex.cpp
Warning: You are using gcc version "4.4.3-4ubuntu5)". The version
currently supported with MEX is "4.3.4".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
mexTryAlex.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’:
mexTryAlex.cpp:16: error: cannot convert ‘double*’ to ‘double**’ in initialization
mex: compile of ' "mexTryAlex.cpp"' failed.
??? Error using ==> mex at 208
Unable to complete successfully.
編譯錯誤是...? – 2013-04-22 11:24:35
嗯,我想'mxGetPr(prhs [0])'是一個指針(而不是一個指針指針),你會得到一個'double dereference'類型的錯誤? – 2013-04-22 11:26:36
@RodyOldenhuis,我在*編輯*中添加了錯誤信息我只是把 – Vass 2013-04-22 12:00:35