使用单元格数组在C ++ Eigen Matrix和MATLAB mxArray之间传递数据

问题描述 投票:2回答:2

我想在Eigen Matrix / Vector和mex数组之间传递数据。在下面的代码中,我定义了一个名为y_output的mex数组,它包含一个单元数组。变量y_output将传递给MATLAB。 y_output中的每个元素都是一个向量但长度不同的元素。我想传递指向mex数组y_output的特征向量的指针。

请注意,存储在y中的数据将使用用户定义的函数进行修改。调用函数后,我会假设存储在y_output中的数据将被相应修改。但是,我无法直接将指针从y_output传递给y。有没有办法让它成为可能?谢谢!

这个问题与Pass C++ Eigen matrix to Matlab mex output的问题类似但不同。这个问题是询问如何传递一个矩阵数组,而该链接中的问题是询问如何传递矩阵。

#include "mex.h"
#include "matrix.h"
#include <Eigen>


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{


// prhs[0]: a cell array of length T, each element is a vector with different lengths
mwSize T =  mxGetNumberOfElements(prhs[0]);
mwSize* n = new mwSize[T];
Eigen::VectorXd* z = new Eigen::VectorXd[T];

for(int t=0; t<T; t++){
    n[t] = mxGetNumberOfElements(mxGetCell(prhs[0], t));
    z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
}

// create a cell matrix with T rows and one columns
mxArray* y_output = mxCreateCellMatrix(T,1);

// create corresponding Eigen objects
Eigen::VectorXd* y = new Eigen::VectorXd[T]();

Eigen::VectorXd y_temp(n[0]); y_temp.setZero();

for(int t=0; t<T; t++){
    mxSetCell(y_output, t, mxCreateDoubleMatrix(n[t], 1, mxREAL));

    y[t] = Eigen::VectorXd::Zero(n[t]);
    y_temp.resize(n[t]);

    Eigen::Map<Eigen::VectorXd> y[t](mxGetPr(mxGetCell(y_output, t)), n[t]); // This is not correct!



}


// Myfun(y, z);

// set output
plhs[0] = y_output;

}
c++ matlab mex eigen3
2个回答
1
投票

您的解决方案围绕MyFunc的输入和输出复制数据。可以使用std::vectorEigen::Map对象传递两个参数。以下代码基于您的答案。以//--开头的行从代码中删除,并替换为后面的行。

作为旁注:避免使用new分配数组(占所有情况的99.99%),但使用std::vector代替。当对象超出范围时,这将负责释放所有资源。此外,在MyFunc你不必猜测yz的大小。

#include "mex.h"
#include "matrix.h"
//-- #include <Eigen> <-- this should be <Eigen/Eigen>, but likely Eigen/Core suffices
//                        Perhaps you also need to change your include-path 
#include <Eigen/Core>

#include <vector>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

    // prhs[0]: a cell array of length T, each element is a vector with different lengths
    mwSize T =  mxGetNumberOfElements(prhs[0]);
    //-- mwSize* n = new mwSize[T];
    std::vector<mwSize> n(T);
    //-- Eigen::VectorXd* z = new Eigen::VectorXd[T];
    std::vector<Eigen::Map<const Eigen::VectorXd> > z; // input vector of Maps
    z.reserve(T);

    for(int t=0; t<T; t++){
        // Note: You don't actually seem to need n[t], except for creating y_output
        n[t] = mxGetNumberOfElements(mxGetCell(prhs[0], t));
        //-- z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
        z.emplace_back(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
    }

    // create a cell matrix with T rows and one columns
    mxArray* y_output = mxCreateCellMatrix(T,1);

    // create corresponding Eigen objects
    //-- Eigen::VectorXd* y = new Eigen::VectorXd[T]();
    std::vector<Eigen::Map<Eigen::VectorXd> > y; // output vector of Maps
    y.reserve(T);

    // This must be called after setting up y: 
    //-- Myfun(y, z);

    //-- double* ptemp;
    for(int t=0; t<T; t++){
        mxSetCell(y_output, t, mxCreateDoubleMatrix(n[t], 1, mxREAL));

        //-- ptemp = mxGetPr(mxGetCell(y_output, t));
        //-- // assign the data stored in y[t] to the contents in y_output.
        //-- for(int i=0; i<n[t]; i++){
        //--     //mxGetPr(mxGetCell(y_output, t))[i] = y[t](i);
        //--     ptemp[i] = y[t](i);
        //-- }
        y.emplace_back(mxGetPr(mxGetCell(y_output, t)), n[t]);
    }

    // Now call Myfun, but the function now needs to accept vectors of Eigen::Map, instead of pointers to VectorXd
    // It should be possible to keep the Code inside Myfun unchanged
    // Myfun(y, z);

    //-- ptemp = NULL;

    // set output
    plhs[0] = y_output;

}

0
投票

我已经找到了实现我想要的方法。关键的一步是首先调用我自己的函数,最后将y中存储的数据分配给y_output。我在下面添加了修改后的代码,以便对有兴趣的人有所帮助。

#include "mex.h"
#include "matrix.h"
#include <Eigen>


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{


// prhs[0]: a cell array of length T, each element is a vector with different lengths
mwSize T =  mxGetNumberOfElements(prhs[0]);
mwSize* n = new mwSize[T];
Eigen::VectorXd* z = new Eigen::VectorXd[T];

for(int t=0; t<T; t++){
    n[t] = mxGetNumberOfElements(mxGetCell(prhs[0], t));
    z[t] = Eigen::Map<Eigen::VectorXd>(mxGetPr(mxGetCell(prhs[0], t)), n[t]);
}

// create a cell matrix with T rows and one columns
mxArray* y_output = mxCreateCellMatrix(T,1);

// create corresponding Eigen objects
Eigen::VectorXd* y = new Eigen::VectorXd[T]();


 // Myfun(y, z);

double* ptemp;
for(int t=0; t<T; t++){
    mxSetCell(y_output, t, mxCreateDoubleMatrix(n[t], 1, mxREAL));

    ptemp = mxGetPr(mxGetCell(y_output, t));
    // assign the data stored in y[t] to the contents in y_output.
    for(int i=0; i<n[t]; i++){
        //mxGetPr(mxGetCell(y_output, t))[i] = y[t](i);
        ptemp[i] = y[t](i);
    }


}

ptemp = NULL;


// set output
plhs[0] = y_output;

}
© www.soinside.com 2019 - 2024. All rights reserved.