Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Create a moving average

Asked by Dirk

Dirk (view profile)

on 28 Jun 2013

Hi There, How can I calculate a moving average for a column of data. For instance i want to average the 50 points either side of each data point in my column. Thanks

0 Comments

Dirk

Dirk (view profile)

Products

No products are associated with this question.

5 Answers

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 28 Jun 2013
Accepted answer

A - your data

L = filter(ones(101,1)/101,1,[A(:) zeros(50,1)]);
out = L(51:end);

0 Comments

Andrei Bobrov

Andrei Bobrov (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 28 Jun 2013
Edited by Image Analyst

Image Analyst (view profile)

on 28 Jun 2013

For a 1D column vector:

movingAverage = conv(yourSignal, ones(101,1)/101, 'same');

For a 2D array of columns:

movingAverage = conv2(yourSignal, ones(101,1)/101, 'same');

If you don't want the central pixel to be included in the average and have ONLY the 50 on either side, use

kernel = ones(101,1)/100;
kernel(51) = 0;
movingAverage = conv(yourSignal, kernel, 'same');

Same for a 2D matrix except use conv2 instead of conv. conv() and conv2() are highly optimized and very fast.

0 Comments

Image Analyst

Image Analyst (view profile)

Answer by Marc

Marc (view profile)

on 28 Jun 2013

If you have the financial toolbox, doc movavg()....

[Short, Long] = movavg(Asset, Lead, Lag, Alpha)

0 Comments

Marc

Marc (view profile)

Answer by the cyclist

the cyclist (view profile)

on 28 Jun 2013

This page of the MATLAB documentation has an example of using the filter() command to calculate a moving average:

http://www.mathworks.com/help/matlab/data_analysis/filtering-data.html

1 Comment

Dirk

Dirk (view profile)

on 28 Jun 2013

Thanks. The filter function is set to average data from the previous n measurements. Is there a simple way to specify a different averaging criteria? Thanks

the cyclist

the cyclist (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 28 Jun 2013

There are many moving average filters in the FileExchange. Whenever a standard problem occurs, looking in the FEX is a good idea:

http://www.mathworks.com/matlabcentral/fileexchange/index?utf8=%E2%9C%93&term=moving+average

0 Comments

Jan Simon

Jan Simon (view profile)

Contact us