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