/* C mex version of ind2subv.m in misc directory */ /* 2 input, 1 output */ /* siz, ndx */ /* sub */ #include "mex.h" #include void rbinary(int num, int n, double *rbits){ int i, mask; num = num - 1; mask = 1 << (n-1); /* mask = 00100...0 , where the 1 is in column n (rightmost = col 1) */ for (i = 0; i < n; i++) { rbits[n-i-1] = ((num & mask) == 0) ? 1 : 2; num <<= 1; } } void ind_subv(int num, const double *sizes, int n, double *rbits){ int i; int *cumprod; cumprod = malloc(n * sizeof(int)); num = num - 1; cumprod[0] = 1; for (i = 0; i < n-1; i++) cumprod[i+1] = cumprod[i] * (int)sizes[i]; for (i = n-1; i >= 0; i--) { rbits[i] = ((int)floor(num / cumprod[i])) + 1; num = num % cumprod[i]; } free(cumprod); } void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ int i, j, k, nCol, nRow, nnRow, binary, count, temp, temp1, start; double *pSize, *pNdx, *pr; double ndx; int *subv, *cumprod, *templai; pSize = mxGetPr(prhs[0]); pNdx = mxGetPr(prhs[1]); nCol = mxGetNumberOfElements(prhs[0]); nnRow = mxGetNumberOfElements(prhs[1]); nRow = 1; for(i=0; i 2.0){ binary = 0; break; } else if((int)pSize[i] == 1){ binary = 1; } } if(nnRow == 1){ ndx = mxGetScalar(prhs[1]); plhs[0] = mxCreateDoubleMatrix(1, nCol, mxREAL); pr = mxGetPr(plhs[0]); if(binary == 2)rbinary((int)ndx, nCol, pr); else ind_subv((int)ndx, pSize, nCol, pr); return; } plhs[0] = mxCreateDoubleMatrix(nnRow, nCol, mxREAL); pr = mxGetPr(plhs[0]); subv = malloc(nRow * nCol * sizeof(int)); if (binary == 2) { for(j=0; j> k; } for(j=0; j 2) temp = 1; for(k=0; k (int)pSize[j]) temp = 1; for(k=0; k