Tuesday, April 20, 2010

把c變成mex file

給未來的裕翔

因為有個m file執行一次要九分鐘

忍無可忍之際, 決定改寫作者的c code

那個作者提供的程式不但不完整

c code都不能直接編譯成mex file......

要修改一些地方才可以

vector_t perform_omp(matrix_t a, vector_t b, int k) {

改成

void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]){
matrix_t a;
a.matrix = mxGetPr(prhs[0]);
a.w = mxGetN(prhs[0]);
a.h = mxGetM(prhs[0]);
vector_t b;
b.vector = mxGetPr(prhs[1]);
b.h = mxGetM(prhs[1]);
int k = *mxGetPr(prhs[2]);


然後

return x_sol;

改成

plhs[0] = mxCreateDoubleMatrix(a.w, 1, mxREAL);
x_sol.vector = mxGetPr(plhs[0]);
x_sol.h = mxGetM(plhs[0]);


記得要放面上面宣告的地方

這樣就可以了

接著介紹關於mex file的幾個functionparameter意義

nrhs number of right hand side


prhs pointer of right hand side

nlhsplhs比照辦理

mxGetPr 取得prhs或plhs的實數變數指標, get pointer of real

mxGetM 取得該變數的row數

mxGetN 取得該變數的column數


No comments:

Post a Comment