=matlab 工具 mex= *mex 主要是讓Maltab和C語言、Fortran語言作溝通,Matlab程式中把引數對應好就可以利用C、Fortran幫我們做運算。[[br]] 進入mexfunction我們就可以寫C、Fortran程式了。然而若可以寫C程式,這樣的話我們也可以利用GPU來做運算。[[br]] mexFunction的框架如下: void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { ....可以撰寫C、Fortran程式 } nlhs(Type = int): number of "left hand side" arguments (呼叫函式時,等號左邊的變數數目,即 plhs 之個數)[[br]] plhs(Type = array of pointers to mxArrays): actual output arguments (呼叫函式時,等號左邊之變數本體)[[br]] nrhs(Type = int): number of "right hand side" arguments (呼叫函式時,等號右邊的變數數目,即 prhs 之個數)[[br]] prhs(Type = const array of pointers to mxArrays): all of the pointers to the mxArrays of input data for instance (呼叫函式時,等號右邊之變數本體[[br]] 以下舉例一個範例。利用Matlab輸入兩個向量作相加。 程式碼如下:檔名為add.c #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) { int i, j, mA, nA, mB, nB, nMatSize;[[br]] double *A, *B, *C;[[br]] /*input_check */[[br]] if (nrhs != 2) mexErrMsgTxt("The input must 2"); if (nlhs != 1) mexErrMsgTxt("The input must 1");[[br]] mA = mxGetM(prhs[0]);[[br]] nA = mxGetN(prhs[0]);[[br]] mB = mxGetM(prhs[1]);[[br]] nB = mxGetN(prhs[1]);[[br]] printf("mA= %d\n",mA); [[br]] if (mA != mB ||nA != nB) [[br]] mexErrMsgTxt("The size must identical") ; [[br]] /*output to buffer*/[[br]] plhs[0]=mxCreateDoubleMatrix(mA,nA,mxREAL);[[br]] /*get value*/[[br]] A = mxGetPr(prhs[0]);[[br]] B = mxGetPr(prhs[1]);[[br]] C = mxGetPr(plhs[0]);[[br]] /*sum*/[[br]] nMatSize = mA * nA;[[br]] for (i=0; i mex add.c [[br]] > a = [1 2 3 4]; [[br]] > b = [1 2 3 4]; [[br]] > c = add(a,b) [[br]] mA=1 [[br]] ending [[br]] c = [[br]] 2 4 6 8 [[br]]