annotate toolboxes/FullBNT-1.0.7/KPMtools/normaliseC.c @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 /* C mex version of normalise.m in misc directory */
wolffd@0 2
wolffd@0 3 #include "mex.h"
wolffd@0 4
wolffd@0 5 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
wolffd@0 6 {
wolffd@0 7 double *T, *sum_ptr, sum;
wolffd@0 8 int i, N;
wolffd@0 9
wolffd@0 10 plhs[0] = mxDuplicateArray(prhs[0]);
wolffd@0 11 T = mxGetPr(plhs[0]);
wolffd@0 12 if(mxIsSparse(plhs[0])) N = mxGetJc(plhs[0])[mxGetN(plhs[0])];
wolffd@0 13 else N = mxGetNumberOfElements(plhs[0]);
wolffd@0 14
wolffd@0 15 plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
wolffd@0 16 sum_ptr = mxGetPr(plhs[1]);
wolffd@0 17
wolffd@0 18 sum = 0;
wolffd@0 19 for (i = 0; i < N; i++) {
wolffd@0 20 sum += *T++;
wolffd@0 21 }
wolffd@0 22 T = mxGetPr(plhs[0]);
wolffd@0 23 if (sum > 0) {
wolffd@0 24 for (i = 0; i < N; i++) {
wolffd@0 25 *T++ /= sum;
wolffd@0 26 }
wolffd@0 27 }
wolffd@0 28 *sum_ptr = sum;
wolffd@0 29 }