Create a moving average
이전 댓글 표시
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
2020년 3월 27일
You can use Moving Average Function in the FileExchange. Please find the link below:
채택된 답변
추가 답변 (5개)
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
2017년 11월 29일
Thanks. Why do you divide by 101?
Image Analyst
2017년 11월 29일
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
2017년 11월 30일
So you could use ones(101,1) first, and onces it is convolved you can divide by 101?
Image Analyst
2017년 11월 30일
Yes.
Grzegorz Knor
2017년 4월 7일
편집: Adam Danz
2021년 9월 19일
3 개 추천
댓글 수: 1
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
2013년 6월 28일
1 개 추천
If you have the financial toolbox, doc movavg()....
[Short, Long] = movavg(Asset, Lead, Lag, Alpha)
the cyclist
2013년 6월 28일
1 개 추천
This page of the MATLAB documentation has an example of using the filter() command to calculate a moving average:
Jan
2013년 6월 28일
1 개 추천
There are many moving average filters in the FileExchange. Whenever a standard problem occurs, looking in the FEX is a good idea:
카테고리
도움말 센터 및 File Exchange에서 Correlation and Convolution에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!