MATLAB Answers

Create a moving average

조회 수: 533(최근 30일)
Dirk
Dirk 28 Jun 2013
댓글: arman arefi 27 Mar 2020
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

  댓글 수: 1

arman arefi
arman arefi 27 Mar 2020
You can use Moving Average Function in the FileExchange. Please find the link below:

Sign in to comment.

채택된 답변

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

  댓글 수: 0

Sign in to comment.

추가 답변(5개)

Image Analyst
Image Analyst 28 Jun 2013
편집: Image Analyst 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.

  댓글 수: 4

표시 이전 댓글 수: 1
Image Analyst
Image Analyst 29 Nov 2017
You divide by however many 1's there are in the kernel. If you don't then you're not getting the average. Remember the average is the sum divided by the number of elements in the sum. If you didn't have 101, then you'd simply be summing 101 values and the resulting image would be 101 times as bright rather than in the same intensity range as the original.
Nuchto
Nuchto 30 Nov 2017
So you could use ones(101,1) first, and onces it is convolved you can divide by 101?
Image Analyst
Image Analyst 30 Nov 2017
Yes.

Sign in to comment.


Grzegorz Knor
Grzegorz Knor 7 Apr 2017
From MATLAB R2016a there is a function movmean which does not require additional toolboxes.

  댓글 수: 1

Image Analyst
Image Analyst 7 Apr 2017
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.

Sign in to comment.


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

  댓글 수: 0

Sign in to comment.


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

  댓글 수: 1

Dirk
Dirk 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

Sign in to comment.


Jan
Jan 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:

  댓글 수: 0

Sign in to comment.


Translated by