Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/KPMtools/subv2indKPM.c @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 /* C mex version of subv2ind*/ | |
2 /* 2 inputs, 1 output */ | |
3 /* siz, subv */ | |
4 /* ndx */ | |
5 #include "mex.h" | |
6 | |
7 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ | |
8 int i, j, k, nCol, nRow, binary, temp; | |
9 double *pSize, *pSubv, *pr; | |
10 int *cumprod; | |
11 | |
12 pSize = mxGetPr(prhs[0]); | |
13 pSubv = mxGetPr(prhs[1]); | |
14 nCol = mxGetNumberOfElements(prhs[0]); | |
15 nRow = mxGetM(prhs[1]); | |
16 | |
17 | |
18 if(mxIsEmpty(prhs[1])){ | |
19 plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); | |
20 return; | |
21 } | |
22 | |
23 if(mxIsEmpty(prhs[0])){ | |
24 plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); | |
25 *mxGetPr(plhs[0]) = 1; | |
26 return; | |
27 } | |
28 | |
29 binary = 2; | |
30 for (i = 0; i < nCol; i++){ | |
31 if (pSize[i] > 2.0){ | |
32 binary = 0; | |
33 break; | |
34 } | |
35 else if(pSize[i] == 1.0){ | |
36 binary = 1; | |
37 } | |
38 } | |
39 | |
40 plhs[0] = mxCreateDoubleMatrix(nRow, 1, mxREAL); | |
41 pr = mxGetPr(plhs[0]); | |
42 for(i=0; i<nRow; i++){ | |
43 pr[i] = 1.0; | |
44 } | |
45 | |
46 if (binary == 2){ | |
47 for(j=0; j<nCol; j++){ | |
48 temp = j * nRow; | |
49 for(i=0; i<nRow; i++){ | |
50 pr[i] += ((int)pSubv[temp + i] - 1) << j; | |
51 } | |
52 } | |
53 } | |
54 else if(binary == 1){ | |
55 cumprod = malloc(nCol * sizeof(int)); | |
56 cumprod[0] = 1; | |
57 for(i=1; i<nCol; i++){ | |
58 k = (int)pSize[i-1] - 1; | |
59 cumprod[i] = cumprod[i-1] << k; | |
60 } | |
61 for(j=0; j<nCol; j++){ | |
62 temp = j * nRow; | |
63 for(i=0; i<nRow; i++){ | |
64 k = (int)pSubv[temp + i] - 1; | |
65 if(k)pr[i] += cumprod[j]; | |
66 } | |
67 } | |
68 free(cumprod); | |
69 } | |
70 else { | |
71 cumprod = malloc(nCol * sizeof(int)); | |
72 cumprod[0] = 1; | |
73 for(i=1; i<nCol; i++){ | |
74 k = (int)pSize[i-1]; | |
75 cumprod[i] = cumprod[i-1] * k; | |
76 } | |
77 for(j=0; j<nCol; j++){ | |
78 temp = j * nRow; | |
79 for(i=0; i<nRow; i++){ | |
80 k = (int)pSubv[temp + i] - 1; | |
81 pr[i] += cumprod[j] * k; | |
82 } | |
83 } | |
84 free(cumprod); | |
85 } | |
86 } | |
87 | |
88 | |
89 |