昨天用 Matlabmex做了个函数,觉得很方便,这里把方法简单说下,供大家参考
为什么要用 Mex,因为可能在 matlab里面有些繁琐的循环,会耗尽你的时间,
要知道,matlab最不擅长的就是做 for循环了,最擅长的是矩阵运算,因此我
们可以调用 C 语言的动态链接库来帮我们计算一些 matlab中比较费时的循环。
实验证明,C 算的真快。。。。
#include "mex.h";
#include <math.h>
void ComputKernel(double gamma,intnRow,intnCol,const double *Src,double
*Dst)
double sum,temp;
int i,j,s,index1,index2;
for (i=0;i<nRow;i++)
for (j=i;j<nRow;j++)
index1 = i+s*nRow;
index2 = index1 -i +j;
temp = Src[index1] - Src[index2];
sum += temp*temp;
temp = Src[i]*Src[j]*exp(-gamma*sum);;
Dst[i+j*nRow] = temp;
Dst[j+i*nRow] = temp;
}
Dst[i+j*nRow] = Src[i]*Src[j];
void mexFunction(intnlhs,mxArray*plhs[],intnrhs,constmxArray*prhs[])
{