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

Answer by Andrei Bobrov
on 28 Jun 2013

Accepted Answer

A - your data

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

out = L(51:end);

Sign in to comment.

Answer by Image Analyst
on 28 Jun 2013

Edited by Image Analyst
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.

Image Analyst
on 29 Nov 2017

Nuchto
on 30 Nov 2017

So you could use ones(101,1) first, and onces it is convolved you can divide by 101?

Image Analyst
on 30 Nov 2017

Yes.

Sign in to comment.

Answer by Grzegorz Knor
on 7 Apr 2017

Image Analyst
on 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.

Answer by the cyclist
on 28 Jun 2013

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.