Search code examples
matlablinear-algebramexarmadillomatrix-inverse

Matlab + Armadillo and the inverse matrix crashes


I'm trying calculate a inverse matrix from Matlab using Armadillo lib. To do this I'm using Mex. Unfortunately, Matlab crashes when I call the function. Looking at my code, someone can help where am I wrong?

#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)); 
}

It compiles without errors.


Solution

  • Try this:

    #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))); 
    
    }