フィルターのクリア

How to calculate moving average

248 ビュー (過去 30 日間)
Mekala balaji
Mekala balaji 2017 年 11 月 16 日
編集済み: dpb 2020 年 11 月 25 日
Hi,
I have following data:
0.10
0.24
0.30
0.25
0.33
0.35
0.46
0.47
0.48
0.51
0.52
0.53
0.53
0.57
0.58
0.58
0.58
0.59
0.63
0.64
0.66
0.72
I want to take 3-points moving average,
Please help some one
  2 件のコメント
Quin Silk
Quin Silk 2019 年 12 月 11 日
Can someone write code to do it without using movmean?
Star Strider
Star Strider 2019 年 12 月 11 日
@Quin Silk — See the Moving Average Filter link in my Answer.

サインインしてコメントする。

回答 (6 件)

Bandar
Bandar 2019 年 12 月 11 日
Take a look this data
file=[1 2 3 4];
movmean(file,3)
which returns
1.5000 2.0000 3.0000 3.5000
The filter works as follows:
1 2 (1+2)/2 = 1.5 when k points at 1
1 2 3 (1+2+3)/3 = 2.0 when k points at 2
2 3 4 (2+3+4)/3 = 3.0 when k points at 3
3 4 (3+4)/2 = 3.5 when k points at 4
Now it is easy to convert it to a logical code or merely use movmean().
  3 件のコメント
Bandar
Bandar 2019 年 12 月 11 日
A=[1 2 3 4 5 6];
m=ones(1,length(A));
for i=1:length(A)
if i == 1
m(i) = (A(i)+A(i+1))/2;
elseif i == length(A)
m(i) = (A(i-1)+A(i))/2;
else
m(i) = (A(i-1)+A(i)+A(i+1))/3;
end
end
movmean(A,3)
m
Micheal Omojola
Micheal Omojola 2020 年 9 月 8 日
@Bandar Aldhafeeri: Thank you! Your answer was helpful.

サインインしてコメントする。


KL
KL 2017 年 11 月 16 日
If you have 2016b or later, use movmean,
mean_arr = movmean(your_array,3)

Star Strider
Star Strider 2017 年 11 月 16 日
Use the movmean (link) function (introduced in R2016a).
You can also use the filter function. See the section on Moving-Average Filter (link).
  1 件のコメント
Pallavi Bharati
Pallavi Bharati 2020 年 11 月 25 日
How to do centered moving average ...three point centred moving average in matlab without the movmean command as i have matlab 2015

サインインしてコメントする。


dpb
dpb 2017 年 11 月 16 日

Sudhakar Rayabarapu
Sudhakar Rayabarapu 2018 年 9 月 8 日
編集済み: Sudhakar Rayabarapu 2018 年 9 月 8 日
Name your data array A; and keep as a column; then use this command
OutPut = tsmovavg(A, 's', 3, 1)
this will give you the simple moving average

Pallavi Bharati
Pallavi Bharati 2020 年 11 月 24 日
Can anyone help me to compute three point moving average of a 5 year data.I used the filter command but the result are erroneous .I am using MATLAB 2015.And I have a huge data 5 year day wise data and i have to compute three point moving average for each month .
  2 件のコメント
Pallavi Bharati
Pallavi Bharati 2020 年 11 月 24 日
Using looping can be erroneous for so large data please suggest something else
dpb
dpb 2020 年 11 月 24 日
編集済み: dpb 2020 年 11 月 25 日
The results aren't "erroneous" (presuming you defined the coefficients correctly), they just depend on how one treats the end conditions...examine the following:
>> x=1:10;
>> movmean(x,3)
ans =
1.5000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 9.5000
>> filter(ones(1,3)/3,1,x)
ans =
0.3333 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000
>> conv(x,ones(1,3)/3,'same')
ans =
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 6.3333
>> conv(x,ones(1,3)/3,'valid')
ans =
2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000
>>
movmean special-cases the end positions to change the divisor to match the number of elements from 2:N on each end; filter and conv do not.
It's up to you to determine the solution you wish to use; none is theoretically any more or less correct than any other; just different assumptions are made.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by