inverse matrix in mexFunction
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
void inverseMatrix(int dim, double *matrix, double *invMatrix)
{
// matrix and invMatrix are columnwise.
int *IPIV, LWORK, INFO, i;
double *WORK;
mexPrintf("entered inverseMatrix");
IPIV = mxMalloc((dim+1)*sizeof(int));
LWORK = dim*dim;
WORK = mxMalloc(LWORK*sizeof(double));
for (i=0;i<dim*dim;i++){
invMatrix[i] = matrix[i];
}
mexPrintf("before dgetrf");
dgetrf(&dim, &dim, invMatrix, &dim, IPIV, &INFO);
mexPrintf("before dgetri");
dgetri(&dim, invMatrix, &dim, IPIV, WORK, &LWORK, &INFO);
mxFree(IPIV);
mxFree(WORK);
return;
}
I am trying to use dgetrf and dgetri to inverse a matrix in C but Matlab crashes after successfully giving the correct answer 2 times (I did an interation to try the stability of the c code).
0 comentarios
Respuesta aceptada
Jan
el 4 de Abr. de 2012
The copy of the data to the output would be faster using memcpy:
plhs[0] = mxCreateDoubleMatrix(outputRowLen, outputColLen, mxREAL);
memcpy(mxGetPr(plhs[0]), Sigma_Psi_inv_t,
outputRowLen*outputColLen * sizeof(double));
But you can use the data of the output array directly:
plhs[0] = mxCreateDoubleMatrix(rowSigma_Psi, colSigma_Psi, mxREAL);
mexPrintf("entering inverseMatrix");
inverseMatrix(rowSigma_Psi, arraySigma_Psi, mxGetPr(plhs[0]));
Do not use int for LAPACK calls in an 64 bit environment. Use mwSignedIndex instead, which is a 64 bit integer.
5 comentarios
Jan
el 4 de Abr. de 2012
mwSignedIndex is defined in tmwtypes.h, which is included by mex.h. So "#include mex.h" should be enough.
Más respuestas (3)
Titus Edelhofer
el 4 de Abr. de 2012
Hi Jane,
the code looks fine to me. Maybe it's the calling mex file causing the problem?
Titus
Ver también
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!