简体   繁体   English

将C ++本征矩阵传递给Matlab mex输出

[英]Pass C++ Eigen matrix to Matlab mex output

How can I pass an Eigen Matrix as a Matlab output parameter? 如何将本征矩阵作为Matlab输出参数传递?

I tried this from [EIGEN] How to get in and out data from Eigen matrix: 我从[EIGEN]尝试过此方法。 如何从Eigen矩阵获取数据:

MatrixXd resultEigen;   // Eigen matrix with some result (non NULL!)
double *resultC;                // NULL pointer
Map<MatrixXd>( resultC, resultEigen.rows(), resultEigen.cols() ) = resultEigen;

But it lack information, how to pass the info in resultC to plhs[0] ? 但是它缺少信息,如何将resultC中的信息传递给plhs [0]? Also, when I run the code using this Map, Matlab closes. 另外,当我使用此Map运行代码时,Matlab也将关闭。

You need to first allocate the output MATLAB array, then create an Eigen::Map around it: 您需要首先分配输出MATLAB数组,然后围绕它创建一个Eigen::Map

MatrixXd resultEigen; // Eigen matrix with some result (non NULL!)
mwSize rows = resultEigen.rows();
mwSize cols = resultEigen.cols();
plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL); // Create MATLAB array of same size
Eigen::Map<Eigen::MatrixXd> map(mxGetPr(plhs[0]), rows, cols); // Map the array
map = resultEigen; // Copy

What this does is make an Eigen matrix ( map ) that has the MATLAB array (plhs[0]) as data. 这是制作一个以MATLAB数组(plhs [0])为数据的本征矩阵( map )。 When you write into it, you are actually writing into the MATLAB array. 写入时,实际上是在写入MATLAB数组。

Note that you can create this map before doing your Eigen calculations, and use it instead of resultEigen , to avoid that final copy. 请注意,您可以在进行Eigen计算之前创建此映射,并使用它代替resultEigen来避免生成最终副本。

Note also that you can do exactly the same thing with the input arrays. 还要注意,您可以对输入数组执行完全相同的操作。 Just make sure they are of class double (using mxIsDouble ), or things might go horribly wrong... :) 只需确保它们属于double类(使用mxIsDouble ),否则事情可能会发生严重错误……:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM