我试图索引为MATLAB编写外部c函数使用mex来操作矩阵,并且我无法使用多维索引.有一些例子提供
here,但我还没有找到如何解决我在下面描述的问题.
我有一个样本矩阵:
>> mat
mat =
1 10
2 20
3 30
4 40
5 50
目前我通过矩阵使用线性索引:
#include
#include
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<
cout<< ncols<
int mm, nn;
for (nn=0;nn
for (mm=0;mm
cout << graph_list[nn*(mrows) +mm] <
}
}
}
这会产生:
>> mexTryAlex(mat)
5 rows
2 cols
1
2
3
4
5
10
20
30
40
50
当我更改graph_list的定义并尝试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.