我想索引编写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