Code covered by the BSD License  

Highlights from
Moving averages / Moving median etc

3.9 | 18 ratings Rate this file 78 Downloads (last 30 days) File Size: 1.66 KB File ID: #8251
image thumbnail

Moving averages / Moving median etc


Aslak Grinsted


09 Aug 2005 (Updated )

Calculates moving averages (or median/fun) of a timeseries.

| Watch this File

File Information

MOVING will compute moving averages of order n (best taken as odd)
 Usage: y=moving(x,n[,fun])
 where x is the input vector (or matrix) to be smoothed.
       m is number of points to average over (best odd, but even works)
       y is output vector of same length as x
       fun (optional) is a custom function rather than moving averages
  Note:if x is a matrix then the smoothing will be done 'vertically'.
  hold on;
  legend('x','7pt moving mean','7pt moving median','7pt moving max','location','best')


This file inspired Nth Octave Test Signal, Nth Oct Hand Arm & Ac Filter Tool Box, Continuous Sound And Vibration Analysis, Calibrated Spectral Analysis, Running Percentile, and Downsample Ts.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (27)
23 Apr 2014 Mariangel


MatlabR2002b does not have smooth function so this one helps a lot!! thanks!!

20 Mar 2014 dydy m

dydy m

I tried similar to this script for other data, but error found :
??? Undefined function or method 'moving' for input arguments of type 'double'. How do I change type of data?

Comment only
10 Oct 2013 Juan


Works perfectly. Thank You!

03 Oct 2013 Bartek Luks

Bartek Luks

03 Aug 2013 Chad Greene

Chad Greene

Works great.

24 Apr 2013 Codrut Dan

Codrut Dan

25 Jan 2013 maysam



Comment only
25 Jan 2013 maysam


thanks. It works very well. super.

09 Oct 2012 Freddy Mendoza

Freddy Mendoza

Works genial, thank you for sharing!!

06 Oct 2012 Evan


At first, tests show that this function is faster than Matlab's "tsmovavg" function. But when you look at the result of this function, the output series needs to be moved by "floor( m /2 )". When this is taken into consideration, the entire function becomes much slower than Matlab's default function.

In addition, Matlab's tsmovavg function also takes the averages of multiple series simultaneously (matrices of m series X N data points)

The above was tested for "simple" moving averages.

18 Jun 2012 Mar


This works great! It turns out I also need standard deviation's for the averages. Is it possible to easily modify your code to include this? or do you have any recommendations? Thank you so much!

09 Jun 2012 hussen



I am new in using matlab. I am using it for trading rule such filter and moving average but I could not make it. does anyone have them already programmed in matlab.


Comment only
14 Feb 2012 Olesya


14 Feb 2012 Olesya


Works great, thanks so much for posting!

Comment only
02 Nov 2011 Sherif


Works well, thanks.

13 May 2010 Z Izham

Z Izham

Better than Matlab's moving average function, simple and does the trick

22 Jul 2009 Daniel Høyer Iversen

Daniel Høyer Iversen

27 Jul 2008 P. Strömholm

works great and is very fast. thanks

07 Jul 2008 Aslak Grinsted

Hi. I've updated the script to make it much more flexible. You can now easily calculate things like the moving 90th percentile.

For people with errors: i would appreciate if you email me your problems directly so that i can improve the code.

Comment only
21 Feb 2007 Johannes Driessen

(Sorry - error reported is as follows:)

??? Subscript indices must either be real positive integers or logicals.

Comment only
21 Feb 2007 Johannes Driessen

Error in ==> moving at 27

Comment only
31 Aug 2006 Matteo De Felice

not work

21 Jun 2006 Felix Jose

The script is excellent for wave data smoothing

01 Mar 2006 Aslak Grinsted

Just delete it then. I'll still use it myself.

Comment only
22 Feb 2006 kývanç kýlýç

not work

06 Oct 2005 Aslak Grinsted

moving can handle multiple columns at once which smooth can not. Smooth always uses an odd size window, even if you ask for an even sized one. They also handle edges a little differently. With moving.m it is very easy to spot where the edge is affecting the average.

So, i think smooth does not make moving obsolete.

Comment only
03 Oct 2005 N. Schneebly

use Matlabs smooth function

16 Jun 2011

added Screenshot, updated description

Contact us