Create a moving average

63 次查看(过去 30 天)
Dirk
Dirk 2013-6-28
编辑: Adam Danz 2021-9-19
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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2013-6-28
A - your data
L = filter(ones(101,1)/101,1,[A(:) zeros(50,1)]);
out = L(51:end);

更多回答(5 个)

Image Analyst
Image Analyst 2013-6-28
编辑:Image Analyst 2013-6-28
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.
  4 个评论
Nuchto
Nuchto 2017-11-30
So you could use ones(101,1) first, and onces it is convolved you can divide by 101?

请先登录,再进行评论。


Grzegorz Knor
Grzegorz Knor 2017-4-7
编辑:Adam Danz 2021-9-19
From MATLAB R2016a there is a function movmean which does not require additional toolboxes.
  1 个评论
Image Analyst
Image Analyst 2017-4-7
True, and it offers some edge handling options ('shrink', 'discard', 'fill') that conv2() does not have.
conv2() also does not require any toolboxes because it's in base MATLAB.

请先登录,再进行评论。


Marc
Marc 2013-6-28
If you have the financial toolbox, doc movavg()....
[Short, Long] = movavg(Asset, Lead, Lag, Alpha)

the cyclist
the cyclist 2013-6-28
This page of the MATLAB documentation has an example of using the filter() command to calculate a moving average:
  1 个评论
Dirk
Dirk 2013-6-28
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

请先登录,再进行评论。


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

Community Treasure Hunt

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

Start Hunting!

Translated by