File Exchange

image thumbnail

Moving averages / Moving median etc

version (1.66 KB) by Aslak Grinsted
Calculates moving averages (or median/fun) of a timeseries.


Updated 16 Jun 2011

View Version History

View License

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')

Cite As

Aslak Grinsted (2021). Moving averages / Moving median etc (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (34)

Valeri Disko

André Sprengers

André Sprengers

Bijan Nemati

Very nicely written.

Petra Franjic

Hossein Esmaeili

Chad Greene

dydy m: The Undefined function error most often means Matlab can't find the function. The function may still be in your downloads folder. Put it in your current folder or add a path to wherever you keep it.


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

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?


Works perfectly. Thank You!

Bartek Luks

Chad Greene

Works great.

Codrut Dan




thanks. It works very well. super.

Freddy Mendoza

Works genial, thank you for sharing!!


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.


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!



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.




Works great, thanks so much for posting!


Works well, thanks.

Z Izham

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

Daniel Høyer Iversen

P. Strömholm

works great and is very fast. thanks

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.

Johannes Driessen

(Sorry - error reported is as follows:)

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

Johannes Driessen

Error in ==> moving at 27

Matteo De Felice

not work

Felix Jose

The script is excellent for wave data smoothing

Aslak Grinsted

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

kývanç kýlýç

not work

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.

N. Schneebly

use Matlabs smooth function

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!