annotate _FullBNT/KPMtools/subv2indKPM.c @ 9:4ea6619cb3f5 tip

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