필터 지우기
필터 지우기

Basic Moving Average Calculation

조회 수: 3 (최근 30일)
P_Alpha
P_Alpha 2015년 10월 9일
편집: Mohammad Abouali 2015년 10월 9일
Hi Everyone - Sorry if this question is poorly asked as I am a matlab rookie finally moving away from performing my calcs in excel. I have a 194 row array of stock prices (Ordered from newest to oldest), named "SData". I am trying to calculate the 30 day moving average, but I have been unable to do so and looking at other forums is just confusing me more. I would imagine that this is super basic, so I would appreciate any help. That calcs should stop at row 167 since there is not 30 days worth of prior data. Thanks in advance for your help!
Also, if any of my matlab lingo is off, feel free to berate me for it!

채택된 답변

Mohammad Abouali
Mohammad Abouali 2015년 10월 9일
편집: Mohammad Abouali 2015년 10월 9일
movingAVGResult=conv(SData(:),ones(30,1)/30,'valid');
  댓글 수: 2
P_Alpha
P_Alpha 2015년 10월 9일
Worked like a charm. If not too much trouble, would you please be able to tell me how this works? I am utterly confused! Thanks so much!
Mohammad Abouali
Mohammad Abouali 2015년 10월 9일
편집: Mohammad Abouali 2015년 10월 9일
conv stands for convolution. you can get more info on this on wikipedia.
So when you say conv(A,b) pretty much slides b over A and multiplies element by element and then sums them up. So if you design the kernel properly, i.e. b, you can achieve different goals.
For simplicity, let's say you want to take the average over a window of 2 days, instead of 30 days. so your data is
[d1 d2 d3 d4 ...]
to the moving average with window size two you do:
avg1= (d1+d2)/2 or ([d1 d2]) .* ([1 1]/2)
avg2= (d2+d3)/2 or ([d2 d3]) .* ([1 1]/2)
....
so if you look pretty much you are moving [1 1]/2 over your data.
In your case you had 30 days so there are 30 ones in that bracket or ones(30,1)/30.
Note that the kernel is flipped when using convolution. however, since here we have symmetric kernel it doesn't matter. Cross-correlation does not flip the kernel.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by