#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *p, *pEnd, M;
  
    /* duplicate array from input to output */
    plhs[0] = mxDuplicateArray(prhs[0]);
    
    /* abort if array is empty */
    if (mxGetNumberOfElements(plhs[0]) < 2) {
        return;
    }
    
    /* cycle through elements */
    pEnd = mxGetPr(plhs[0]) + mxGetNumberOfElements(plhs[0]);
    for(p = mxGetPr(plhs[0]) + 1; p < pEnd; p++) {
        if (*p < *(p-1)) {
            *p = *(p-1);
        }
    }
    
    return;
}