How to calculate a 6-month backward looking moving average matrix?
이전 댓글 표시
I have monthly data and would like to calculate a 6-month backward moving average. I'm looking at movavg function. I tried the following:
[Short, Long] = movavg(returns_list, 0, 6);
I got this error "Lead and lag arguments must be positive <= 588."
Should I use "[Short, Long] = movavg(returns_list, 1, 6);" instead?
Thanks, J
답변 (2개)
Image Analyst
2012년 9월 17일
I don't have whatever toolbox has movagv() in it, but I think you could use convolution:
filteredSignal = conv(signal, [0 0 0 0 0 1 1 1 1 1 1]/6, 'valid')
Remember, convolution flips the kernel so that's why you want to have the 1's on the right side of the array to get the average of the current and 5 prior values (assuming each element is one month).
Julio
2012년 9월 17일
0 개 추천
댓글 수: 3
Andrei Bobrov
2012년 9월 17일
As by IA
filteredSignal = conv(signal, [0 0 0 0 0 1 1 1 1 1 1]/6, 'same')
Julio
2012년 9월 17일
Image Analyst
2012년 9월 17일
We don't know what your data are. Months have different numbers of days in them. If you have missing days or missing months, you should first run interp1 on it to fill them in. Conv() has options 'same', 'valid', and 'full' depending on how you want to handle the "edge effects" where your window reaches the beginning and end of your data where you don't have the full 6 months of data.
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!