我正在尝试使用 mex 为 MATLAB 编写一个外部 c++ 函数来索引来操作矩阵,并且无法使用多维索引。这里提供了一些示例,但我还没有找到如何解决我在下面描述的问题。我有一个样本矩阵:
>> mat
mat =
1 10
2 20
3 30
4 40
5 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";
}
}
}
这会产生:
>> mexTryAlex(mat)
5 rows
2 cols
1
2
3
4
5
10
20
30
40
50
当我更改 graph_list 的定义并尝试对 graph_list 进行 2D 索引时,会出现以下编译错误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.