本文介绍了将C ++本征矩阵传递给Matlab mex输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过本征矩阵作为Matlab输出参数?

How can I pass an Eigen Matrix as a Matlab output parameter?

我从 [EIGEN]如何从特征矩阵获取数据:

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

但是它缺少信息,如何将resultC中的信息传递给plhs [0]?另外,当我使用此Map运行代码时,Matlab也将关闭.

But it lack information, how to pass the info in resultC to plhs[0] ?Also, when I run the code using this Map, Matlab closes.

推荐答案

您需要先分配输出MATLAB数组,然后在其周围创建一个Eigen::Map:

You need to first allocate the output MATLAB array, then create an Eigen::Map around it:

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

这是制作一个以MATLAB数组(plhs [0])为数据的本征矩阵(map).写入时,实际上是在写入MATLAB数组.

What this does is make an Eigen matrix (map) that has the MATLAB array (plhs[0]) as data. When you write into it, you are actually writing into the MATLAB array.

请注意,您可以在进行Eigen计算之前创建此地图,并使用它代替resultEigen,以避免产生最终副本.

Note that you can create this map before doing your Eigen calculations, and use it instead of resultEigen, to avoid that final copy.

还请注意,您可以对输入数组执行完全相同的操作.只需确保它们属于double类(使用mxIsDouble),否则事情可能会发生严重错误……:)

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... :)

这篇关于将C ++本征矩阵传递给Matlab mex输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:36