Daniel@0: /* CREATED:2010-01-08 17:25:37 by Brian McFee */ Daniel@0: /* cummax.c Daniel@0: * Daniel@0: * cumulative maximum (analogous to cumsum) Daniel@0: * Daniel@0: * Compile: Daniel@0: * mex -DNAN_EQUALS_ZERO cummax.c Daniel@0: */ Daniel@0: Daniel@0: #include "mex.h" Daniel@0: Daniel@0: Daniel@0: #if NAN_EQUALS_ZERO Daniel@0: #define IsNonZero(d) ((d) != 0.0 || mxIsNan(d)) Daniel@0: #else Daniel@0: #define IsNonZero(d) ((d) != 0.0) Daniel@0: #endif Daniel@0: Daniel@0: void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) Daniel@0: { Daniel@0: /* Declare variables */ Daniel@0: int n; /* number of elements */ Daniel@0: double *pi; /* input array */ Daniel@0: double *po1, *po2; /* output array(s) */ Daniel@0: double currentMax; Daniel@0: double currentMaxPointer; Daniel@0: int i; Daniel@0: Daniel@0: if (nrhs != 1) { Daniel@0: mexErrMsgTxt("Only one input argument is required."); Daniel@0: } Daniel@0: if (nlhs > 2) { Daniel@0: mexErrMsgTxt("Too many output arguments."); Daniel@0: } Daniel@0: Daniel@0: if (!(mxIsDouble(prhs[0]))) { Daniel@0: mexErrMsgTxt("Input array must be of type double."); Daniel@0: } Daniel@0: Daniel@0: n = mxGetNumberOfElements(prhs[0]); Daniel@0: pi = (double *)mxGetPr(prhs[0]); Daniel@0: Daniel@0: plhs[0] = mxCreateDoubleMatrix(1, n, mxREAL); Daniel@0: po1 = mxGetPr(plhs[0]); Daniel@0: Daniel@0: plhs[1] = mxCreateDoubleMatrix(1, n, mxREAL); Daniel@0: po2 = mxGetPr(plhs[1]); Daniel@0: Daniel@0: /* Now for the meat */ Daniel@0: Daniel@0: currentMax = pi[0]; Daniel@0: currentMaxPointer = 1; Daniel@0: for (i = 0; i < n; i++) { Daniel@0: if (pi[i] > currentMax) { Daniel@0: currentMax = pi[i]; Daniel@0: currentMaxPointer = i + 1; Daniel@0: } Daniel@0: po1[i] = currentMax; Daniel@0: po2[i] = currentMaxPointer; Daniel@0: } Daniel@0: Daniel@0: mxSetN(plhs[0], n); Daniel@0: mxSetN(plhs[1], n); Daniel@0: Daniel@0: }