matlab中如何写二维矩阵的循环函数,如何在MATLAB的mex c函数中循环遍历矩阵元素?...

我试图索引为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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值