运行 matlab MEX 文件的时间错误

Run time bug on matlab MEX file

我在执行 MEX 文件时遇到困难,出现 运行 时间错误! 我想给出两个输入:inMatToInv、inMatToMul 我想计算:inv(inMatToInv)*inMatToMul,我假设用户只输入 3x3 矩阵。 我还假设 inMatToInv 是可逆的。

    /*==========================================================
 * inv_and_mul_3by3.c 
 * inverse 3x3 matrix and multiply it by another 3x3 matrix
 * The calling syntax is: outMatrix = (mat_to_inv,mat_to_mul)
 *========================================================*/

#include "mex.h"

/* The computational routine */
void inv_and_mul_3by3(double *mat_to_inv, double *mat_to_mul, double *out)
{
    // Description : out = inv(mat_to_inv)*mat_to_mul
    double *inversed;
    double det;

    /* Calculate the matrix deteminant */
    det = mat_to_inv[0]*(mat_to_inv[4]*mat_to_inv[8]-mat_to_inv[7]*mat_to_inv[5]) - mat_to_inv[3]*(mat_to_inv[1]*mat_to_inv[8]-mat_to_inv[7]*mat_to_inv[2])+mat_to_inv[6]*(mat_to_inv[1]*mat_to_inv[5]-mat_to_inv[4]*mat_to_inv[2]);

    // Calcualte the inversed matrix
    inversed[0] = (mat_to_inv[4]*mat_to_inv[8] - mat_to_inv[7]*mat_to_inv[5])/det;
    inversed[3] = (mat_to_inv[6]*mat_to_inv[5] - mat_to_inv[3]*mat_to_inv[8])/det;
    inversed[6] = (mat_to_inv[3]*mat_to_inv[7] - mat_to_inv[6]*mat_to_inv[4])/det;
    inversed[1] = (mat_to_inv[7]*mat_to_inv[2] - mat_to_inv[1]*mat_to_inv[8])/det;
    inversed[4] = (mat_to_inv[0]*mat_to_inv[8] - mat_to_inv[6]*mat_to_inv[2])/det;
    inversed[7] = (mat_to_inv[6]*mat_to_inv[1] - mat_to_inv[0]*mat_to_inv[7])/det;
    inversed[2] = (mat_to_inv[1]*mat_to_inv[5] - mat_to_inv[4]*mat_to_inv[2])/det;
    inversed[5] = (mat_to_inv[3]*mat_to_inv[2] - mat_to_inv[0]*mat_to_inv[5])/det;
    inversed[8] = (mat_to_inv[0]*mat_to_inv[4] - mat_to_inv[3]*mat_to_inv[1])/det;

    out[0] = inversed[0]*mat_to_mul[0] + inversed[3]*mat_to_mul[1] + inversed[6]*mat_to_mul[2];
    out[1] = inversed[1]*mat_to_mul[0] + inversed[4]*mat_to_mul[1] + inversed[7]*mat_to_mul[2];
    out[2] = inversed[2]*mat_to_mul[0] + inversed[5]*mat_to_mul[1] + inversed[8]*mat_to_mul[2];
    out[3] = inversed[0]*mat_to_mul[3] + inversed[3]*mat_to_mul[4] + inversed[6]*mat_to_mul[5];
    out[4] = inversed[1]*mat_to_mul[3] + inversed[4]*mat_to_mul[4] + inversed[7]*mat_to_mul[5];
    out[5] = inversed[2]*mat_to_mul[3] + inversed[5]*mat_to_mul[4] + inversed[8]*mat_to_mul[5];
    out[6] = inversed[0]*mat_to_mul[6] + inversed[3]*mat_to_mul[7] + inversed[6]*mat_to_mul[8];
    out[7] = inversed[1]*mat_to_mul[6] + inversed[4]*mat_to_mul[7] + inversed[7]*mat_to_mul[8];
    out[8] = inversed[2]*mat_to_mul[6] + inversed[5]*mat_to_mul[7] + inversed[8]*mat_to_mul[8];
}

/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
    double *inMatToInv;             /* 3x3 input matrix that is inversed */
    double *inMatToMul;             /* 3x3 input matrix that multiply the inversed matrix*/ 
    double *outMatrix;              /* 3x3 output matrix */

    /* create pointers to the real data in the input matrix  */
    inMatToInv = mxGetPr(prhs[0]);
    inMatToMul = mxGetPr(prhs[1]);

    /* create the output matrix */
    plhs[0] = mxCreateDoubleMatrix(3,3,mxREAL);

    /* get a pointer to the real data in the output matrix */
    outMatrix = mxGetPr(plhs[0]);

    /* call the computational routine */
    inv_and_mul_3by3(inMatToInv,inMatToMul,outMatrix);
}

任何帮助将不胜感激。 谢谢, 塔米尔

您声明了一个指针double *inversed,但您没有为它分配任何内存。所以当你访问 inversed[0] 时你会得到一个错误。尝试:

double inversed[9];

我建议使用 blas and lapack interface for matrix operations in a C mex file. Have a look at Mathworks documentation on doing this here

您可能会对用于矩阵求逆的 lapack 例程 dgetrfdgetri 以及用于矩阵乘法的 dgemm 感兴趣。 lapack 界面可能非常混乱且难以掌握,但网上有很多关于它们的参考资料。例如,this stack overflow Q/A.