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