comparison toolboxes/FullBNT-1.0.7/KPMtools/ind2subvKPM.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 ind2subv.m in misc directory */
2 /* 2 input, 1 output */
3 /* siz, ndx */
4 /* sub */
5
6 #include "mex.h"
7 #include <math.h>
8
9 void rbinary(int num, int n, double *rbits){
10 int i, mask;
11 num = num - 1;
12
13 mask = 1 << (n-1); /* mask = 00100...0 , where the 1 is in column n (rightmost = col 1) */
14 for (i = 0; i < n; i++) {
15 rbits[n-i-1] = ((num & mask) == 0) ? 1 : 2;
16 num <<= 1;
17 }
18 }
19
20 void ind_subv(int num, const double *sizes, int n, double *rbits){
21 int i;
22 int *cumprod;
23
24 cumprod = malloc(n * sizeof(int));
25 num = num - 1;
26 cumprod[0] = 1;
27 for (i = 0; i < n-1; i++)
28 cumprod[i+1] = cumprod[i] * (int)sizes[i];
29 for (i = n-1; i >= 0; i--) {
30 rbits[i] = ((int)floor(num / cumprod[i])) + 1;
31 num = num % cumprod[i];
32 }
33 free(cumprod);
34 }
35
36
37 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
38 int i, j, k, nCol, nRow, nnRow, binary, count, temp, temp1, start;
39 double *pSize, *pNdx, *pr;
40 double ndx;
41 int *subv, *cumprod, *templai;
42
43 pSize = mxGetPr(prhs[0]);
44 pNdx = mxGetPr(prhs[1]);
45 nCol = mxGetNumberOfElements(prhs[0]);
46 nnRow = mxGetNumberOfElements(prhs[1]);
47
48 nRow = 1;
49 for(i=0; i<nCol; i++){
50 nRow *= (int)pSize[i];
51 }
52
53 if(nCol == 0){
54 plhs[0] = mxDuplicateArray(prhs[1]);
55 return;
56 }
57
58 binary = 2;
59 for (i = 0; i < nCol; i++){
60 if (pSize[i] > 2.0){
61 binary = 0;
62 break;
63 }
64 else if((int)pSize[i] == 1){
65 binary = 1;
66 }
67 }
68
69 if(nnRow == 1){
70 ndx = mxGetScalar(prhs[1]);
71 plhs[0] = mxCreateDoubleMatrix(1, nCol, mxREAL);
72 pr = mxGetPr(plhs[0]);
73 if(binary == 2)rbinary((int)ndx, nCol, pr);
74 else ind_subv((int)ndx, pSize, nCol, pr);
75 return;
76 }
77
78 plhs[0] = mxCreateDoubleMatrix(nnRow, nCol, mxREAL);
79 pr = mxGetPr(plhs[0]);
80
81 subv = malloc(nRow * nCol * sizeof(int));
82
83 if (binary == 2) {
84 for(j=0; j<nCol; j++){
85 temp = (1 << j);
86 temp1 = j * nRow;
87 for(i=0; i<nRow; i++){
88 subv[temp1 + i] = ((i & temp) == 0) ? 1 : 2;
89 }
90 }
91 }
92 else if(binary == 1){
93 cumprod = (int *)malloc(nCol * sizeof(int));
94 templai = (int *)malloc(nCol * sizeof(int));
95 cumprod[0] = 1;
96 templai[0] = nRow;
97 for(i=1; i<nCol; i++){
98 k = (int)pSize[i-1] - 1;
99 cumprod[i] = cumprod[i-1] << k;
100 templai[i] = templai[i-1] >> k;
101 }
102 for(j=0; j<nCol; j++){
103 temp1 = j * nRow;
104 if(pSize[j] == 1.0){
105 for(i=0; i<nRow; i++){
106 subv[temp1 + i] = 1;
107 }
108 }
109 else{
110 temp = 1;
111 count = 0;
112 for(i=0; i<templai[j]; i++){
113 if(temp > 2) temp = 1;
114 for(k=0; k<cumprod[j]; k++){
115 subv[temp1 + count] = temp;
116 count++;
117 }
118 temp++;
119 }
120 }
121 }
122 free(templai);
123 free(cumprod);
124 }
125 else {
126 cumprod = (int *)malloc(nCol * sizeof(int));
127 templai = (int *)malloc(nCol * sizeof(int));
128 cumprod[0] = 1;
129 for(i=1; i<nCol; i++){
130 cumprod[i] = (int)(cumprod[i-1] * pSize[i-1]);
131 }
132 templai[0] = nRow;
133 for(i=1; i<nCol; i++){
134 templai[i] = nRow / cumprod[i];
135 }
136 for(j=0; j<nCol; j++){
137 temp1 = j * nRow;
138 if(pSize[j] == 1.0){
139 for(i=0; i<nRow; i++){
140 subv[temp1 + i] = 1;
141 }
142 }
143 else{
144 temp = 1;
145 count = 0;
146 for(i=0; i<templai[j]; i++){
147 if(temp > (int)pSize[j]) temp = 1;
148 for(k=0; k<cumprod[j]; k++){
149 subv[temp1 + count] = temp;
150 count++;
151 }
152 temp++;
153 }
154 }
155 }
156 free(cumprod);
157 free(templai);
158 }
159
160 count = 0;
161 for(j=0; j<nCol; j++){
162 temp1 = j * nRow;
163 for(i=0; i<nnRow; i++){
164 start = (int)pNdx[i] - 1;
165 pr[count] = subv[temp1 + start];
166 count++;
167 }
168 }
169 free(subv);
170 }
171
172
173
174
175
176