Matlab + Armadillo 和逆矩阵崩溃

Matlab + Armadillo and the inverse matrix crashes

我正在尝试使用 Armadillo 库从 Matlab 计算逆矩阵。为此,我正在使用 Mex。不幸的是,当我调用该函数时,Matlab 崩溃了。查看我的代码,有人可以帮助我哪里错了吗?

#include "armaMex.hpp"

void mexFunction(int nlhs, mxArray *plhs[],
             int nrhs, const mxArray *prhs[]) 
{   
    mat A = armaGetPr(prhs[0]);
    plhs[0] = armaCreateMxMatrix(A.n_rows,A.n_cols);
    armaSetPr(plhs[0],inv(A)); 
}

编译没有错误。

试试这个:

#include "armaMex.hpp"

void mexFunction(int nlhs, mxArray *plhs[],
         int nrhs, const mxArray *prhs[]) 
{   
    mat A =  conv_to<mat>::from(armaGetPr(prhs[0],true));

    plhs[0] = armaCreateMxMatrix(A.n_rows,A.n_cols, mxDOUBLE_CLASS, mxREAL);
    armaSetPr(plhs[0],conv_to<mat>::from(inv(A))); 

}