1 | /* |
---|
2 | distfunc.c - L1 metric between samples and prototypes |
---|
3 | |
---|
4 | (c) 2004 Pavel Paclik, pavel@ph.tn.tudelft.nl |
---|
5 | |
---|
6 | $Id: distfunc.c,v 1.2 2004/03/03 13:13:23 pavel Exp $ |
---|
7 | |
---|
8 | in order to make a MEX file in Matlab use: |
---|
9 | >> mex distfunc.c -lmatlb |
---|
10 | |
---|
11 | */ |
---|
12 | |
---|
13 | #include <mex.h> |
---|
14 | #include <memory.h> |
---|
15 | #include <string.h> |
---|
16 | |
---|
17 | void mexFunction( |
---|
18 | int nlhs, mxArray *plhs[], |
---|
19 | int nrhs, const mxArray *prhs[] |
---|
20 | ) |
---|
21 | { |
---|
22 | double *wp1, *wp2; |
---|
23 | double* pd; |
---|
24 | double dtemp; |
---|
25 | int fc,sc, |
---|
26 | pc; /* number of prototypes */ |
---|
27 | mxArray *arr,*arr2, *aout; /* input matrices */ |
---|
28 | |
---|
29 | int i,j,k; /* iterators */ |
---|
30 | int is_full_matrix=0; /* flag if we compute the whole sqaure matrix */ |
---|
31 | |
---|
32 | arr=(mxArray*)mlfTranspose(prhs[0]); |
---|
33 | sc=mxGetN(arr); |
---|
34 | fc=mxGetM(arr); |
---|
35 | |
---|
36 | if( nrhs == 1 ) { |
---|
37 | arr2=arr; |
---|
38 | is_full_matrix=1; /* we may compute just half of the matrix */ |
---|
39 | } else { |
---|
40 | arr2=(mxArray*)mlfTranspose(prhs[1]); |
---|
41 | } |
---|
42 | |
---|
43 | pc=mxGetN(arr2); |
---|
44 | if( fc != mxGetM(arr2) ) { |
---|
45 | mexErrMsgTxt("input matrices must have the same number of columns\n"); |
---|
46 | return; |
---|
47 | } |
---|
48 | |
---|
49 | /* |
---|
50 | if( mxGetN(prhs[nrhs-1])==mxGetM(prhs[nrhs-1]) & mxGetN(prhs[nrhs-1])==1 ) |
---|
51 | { |
---|
52 | i=(int)*mxGetPr(prhs[nrhs-1]); |
---|
53 | if(i>=0 & i<3) debug_level=i; |
---|
54 | mexPrintf(" debug_level=%d,%d ",debug_level,i); |
---|
55 | } |
---|
56 | */ |
---|
57 | |
---|
58 | /* create output distance matrix */ |
---|
59 | aout=mxCreateDoubleMatrix(sc,pc,mxREAL); |
---|
60 | pd=mxGetPr(aout); |
---|
61 | |
---|
62 | wp1=mxGetPr(arr); |
---|
63 | wp2=mxGetPr(arr2); |
---|
64 | /* mexPrintf(" %f, %f | %f, %f \n ",wp1[fc],wp1[fc+1],wp2[0],wp2[1]); */ |
---|
65 | |
---|
66 | for(i=0; i<pc; i++) |
---|
67 | { |
---|
68 | for(j=0; j<sc; j++, pd++) |
---|
69 | { |
---|
70 | /* pointers to start of feature vectors */ |
---|
71 | wp1=mxGetPr(arr); wp1+=fc*j; |
---|
72 | wp2=mxGetPr(arr2); wp2+=fc*i; |
---|
73 | for( k=0; k<fc; k++, wp1++, wp2++ ) |
---|
74 | { |
---|
75 | dtemp=(*wp1) - (*wp2); |
---|
76 | /* absolute value */ |
---|
77 | dtemp= dtemp<0.0?-dtemp:dtemp; |
---|
78 | *pd += dtemp; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | plhs[0]=aout; |
---|
84 | |
---|
85 | return; |
---|
86 | } |
---|