Matlab: Mex-File - Matrix Multiplication

조회 수: 17 (최근 30일)
alty
alty 2016년 5월 26일
댓글: Steven Lord 2016년 5월 26일
Hello everyone,
I am looking for a way to implement a matrix multiplication (2 square matrices) using the mex-function.
This is my code so far:
#include <mex.h>
/* core */
double MM(double A, double B, double C, double ii, double jj, double kk, double n, double A[10][10], double B[10][10], double C[10][10])
// double n;
// double A[10][10];
// double B[10][10];
// double C[10][10];
{
for (ii = 1; ii = n; ii ++){
for (jj = 1; jj = n; jj ++){
for (kk = 1; kk = n; kk ++){
C( ii , jj ) = C( ii , jj ) + A( ii , kk ) * B( kk , jj ) ;
}
}
}
return C;
}
/* mex-function */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double A, B, C, ii, jj, kk, n ;
double *result;
/* error-message */
if (nrhs < 2) mexErrMsgTxt("Error: missing input arguments!");
/* warning-message */
else if (nrhs > 2) mexWarnMsgTxt("too many input arguments!");
/* type check */
if ((!mxIsCell(prhs[0])) || (!mxIsCell(prhs[1]))) {
mexErrMsgTxt("Both inputs have to be type cell!");
}
if (sizeof(prhs[0])!= sizeof(prhs[1])) {
mexErrMsgTxt("not the same input size!");
}
A = *mxGetPr(prhs[0]);
B = *mxGetPr(prhs[1]);
n = sizeof(prhs[0]);
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
result = mxGetPr(plhs[0]);
result [0]= MM(A,B);
}
Since I am new to using c in matlab, any help would be deeply appreciated! I always get error messages but I just can not figure out where its going wrong.
Thank you so much!
  댓글 수: 2
James Tursa
James Tursa 2016년 5월 26일
편집: James Tursa 2016년 5월 26일
This code has a lot of errors in it. It kinda looks like you took some code that was written to multiply two scalars and return a scalar result, and then naively tried to expand it to do matrix multiplication. Is that what happened here? To fix this, you need to switch all of that scalar stuff to pointers, and then calculate the indexing into the matrices manually. I can do that, but maybe as a first step you should get that scalar multiply version working first since it will be much simpler. And then only consider doing a matrix multiply version of this once you are very comfortable working with pointers in C. Let me know.
Steven Lord
Steven Lord 2016년 5월 26일
Why are you trying to implement matrix multiplication in a MEX-file yourself? Is there a reason you can't or don't want to:
  1. Use the * operator in MATLAB
  2. Call mexCallMATLAB from your MEX-file to have MATLAB call "mtimes"

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by