📄 kmatrix.c
字号:
/* -------------------------------------------------------------------- [K] = kmatrix( data, ker, arg ) Computes kernel matrix. Make executable file by a command 'mex kmatrix.c kernel.c'. mandatory inputs: data [D x N ] matrix of N training D-dimensional patterns. ker [string] kernel identifier: 'linear', 'poly', 'rbf'. arg [real] kernel argument. mandatory outputs: K [D x D] kernel matrix.
Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac (c) Czech Technical University Prague, http://cmp.felk.cvut.cz Written Vojtech Franc (diploma thesis) 02.11.1999
Modifications:
21-October-2001, V.Franc. 30-September-2001, V.Franc, created. -------------------------------------------------------------------- */#include "mex.h"#include "matrix.h"#include <math.h>#include <stdlib.h>#include <string.h>#include "kernel.h"/* case insensitive string comparision */#ifdef __BORLANDC__ #define STR_COMPARE(A,B,C) strncmpi(A,B,C) /* Borland */#else #define STR_COMPARE(A,B,C) strncasecmp(A,B,C) /* Linux gcc */#endif/* ============================================================== Main MEX function - interface to Matlab.============================================================== */void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ){ long i, j, N; double tmp; double *K; char skernel[10]; /* ---- check number of input arguments ------------- */ if(nrhs < 3) mexErrMsgTxt("Not enough input arguments."); if(nlhs < 1) mexErrMsgTxt("Not enough output arguments."); /* ---- check input arguments ----------------------- */ /* data matrix [dim x N ] */ if( !mxIsNumeric(prhs[0]) || !mxIsDouble(prhs[0]) || mxIsEmpty(prhs[0]) || mxIsComplex(prhs[0]) ) mexErrMsgTxt("Input X must be a real matrix."); /* a string as kernel identifier ('linear',poly','rbf' ) */ if( mxIsChar(prhs[1]) != 1 || mxGetM(prhs[1]) != 1 ) mexErrMsgTxt("Input ker must be a string"); else { /* check which kernel */ mxGetString( prhs[1], skernel, 10 ); if( STR_COMPARE( skernel, "linear", 6) == 0 ) { ker = 0; } else if( STR_COMPARE( skernel, "poly", 4) == 0 ) { ker = 1; } else if( STR_COMPARE( skernel, "rbf", 3) == 0 ) { ker = 2; } else mexErrMsgTxt("Unknown kernel identifier."); } /* real input argument for polynomial and rbf kernel */ if( ker == 1 || ker == 2) { if( !mxIsNumeric(prhs[2]) || !mxIsDouble(prhs[2]) || mxIsEmpty(prhs[2]) || mxIsComplex(prhs[2]) || mxGetN(prhs[2]) != 1 || mxGetM(prhs[2]) != 1 ) mexErrMsgTxt("Input arg must be a real scalar."); else { arg1 = mxGetScalar(prhs[2]); /* take kernel argument */ /* if kernel is RBF than recompute its argument */ if( ker == 2) arg1 = -2*arg1*arg1; } } /* ---- init variables ------------------------------- */ dataA = mxGetPr(prhs[0]); /* pointer at patterns */ dataB = dataA; dim = mxGetM(prhs[0]); /* data dimension */ N = mxGetN(prhs[0]); /* number of data */ /* creates output kernel matrix. */ plhs[0] = mxCreateDoubleMatrix(N,N,mxREAL); K = mxGetPr(plhs[0]); /* computes kenrel matrix. */ for( i = 0; i < N; i++ ) { for( j = i; j < N; j++ ) { tmp = kernel( i, j ); K[i*N+j] = tmp; K[j*N+i] = tmp; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -