Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Moving Along

Ever wish you could easily perform some statistical calculations with a sliding window, but find convolution and filtering a bit daunting for this application? Then a set of new functions in Release R2016a might just be for you!

Contents

New Collection of Functions for Moving Operations

The new collection of functions for moving (or sliding) operations are: These functions complement the cum* functions, some of which have been in MATLAB for a long time, e.g., cumsum, and some of which are relatively new, e.g., * cummax.

Find the sliding maxima

Of course you could always find a local maximum in MATLAB by using a for-loop and working your way down the data, but paying careful attention to the ends. Or, with judicious use of indexing and reshaping, you could get the answer in a more vectorized fashion. Now you can just get it!
ndata = 100;
data = 0.5*rand(ndata,1)+0.5;
npts = 5;
datamvmax = movmax(data, npts);
plot(data,'g*')
axis([0 100 0.4 1.1])
hold on
plot(datamvmax,'b+')
hold off

So much easier now!

Computing moving results is much easier now. And with several options that will also make your computing life better - e.g., choice on handling NaN values, choice of direction, allowing a window that may not be symmetric with respect to lagging and leading values, and how to handle the endpoints. Will these new functions simplify your MATLAB code?

Published with MATLAB® R2016a


  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.