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