使用c ++和mex输出矩阵 [英] Output a matrix with c++ and mex
问题描述
我的c ++代码有问题。我想返回一个矩阵的k维度从我的cpp程序到Matlab。
I have a problem with my c++ code. I want to return a matrix of k-dimensions from my cpp program to Matlab.
我想传递的矩阵存储在 all_data
中,是一个大小为(npoints + 1)x ndims
。
The matrix I want to pass is stored in all_data
, and is a matrix of size (npoints+1) x ndims
.
我一直在寻找如何做到这一点,我想出了:
I have been looking how to do that, and I have come up with:
//send back points
vector< vector <double> > indexes = mxGetPr(plhs[0]);
for (int i=0; i < (npoints1+1); i++)
for (int j=0; j < ndims1; j++)
indexes[ i ][ j ] = all_data[ i ][ j ];
但它不工作,因为 all_data
是向量<向量<双>>
变量,并且matlab说:
But it does not work, as all_data
is a vector<vector<double>>
variable, and matlab says:
error: conversion from 'double*' to non-scalar type
'std::vector<std::vector<double, std::allocator<double> >,
std::allocator<std::vector<double,
std::allocator<double> > > >' requested
有人可以帮助我吗?非常感谢!
Can someone help me out? Thanks a lot!
推荐答案
看起来像mxGetPr会返回一个指向一个双精度数组的指针,矢量的矢量。
It looks like mxGetPr is returning you a pointer to an array of doubles and you are assigning it to a vector of vectors.
这应该可以工作:
double* indexes = mxGetPr(plhs[0]);
这篇关于使用c ++和mex输出矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!