annotate toolboxes/FullBNT-1.0.7/KPMtools/subv2indKPM.c @ 0:cc4b1211e677 tip

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