annotate toolboxes/FullBNT-1.0.7/KPMtools/normaliseC.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 normalise.m in misc directory */
Daniel@0 2
Daniel@0 3 #include "mex.h"
Daniel@0 4
Daniel@0 5 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Daniel@0 6 {
Daniel@0 7 double *T, *sum_ptr, sum;
Daniel@0 8 int i, N;
Daniel@0 9
Daniel@0 10 plhs[0] = mxDuplicateArray(prhs[0]);
Daniel@0 11 T = mxGetPr(plhs[0]);
Daniel@0 12 if(mxIsSparse(plhs[0])) N = mxGetJc(plhs[0])[mxGetN(plhs[0])];
Daniel@0 13 else N = mxGetNumberOfElements(plhs[0]);
Daniel@0 14
Daniel@0 15 plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
Daniel@0 16 sum_ptr = mxGetPr(plhs[1]);
Daniel@0 17
Daniel@0 18 sum = 0;
Daniel@0 19 for (i = 0; i < N; i++) {
Daniel@0 20 sum += *T++;
Daniel@0 21 }
Daniel@0 22 T = mxGetPr(plhs[0]);
Daniel@0 23 if (sum > 0) {
Daniel@0 24 for (i = 0; i < N; i++) {
Daniel@0 25 *T++ /= sum;
Daniel@0 26 }
Daniel@0 27 }
Daniel@0 28 *sum_ptr = sum;
Daniel@0 29 }