Help on calculating cumulative moving average

nc

nc (view profile)

さんによって質問されました 2016 年 10 月 14 日

nc (view profile)

さんによって 編集されました 2017 年 1 月 20 日
Okay I need help , because I have been trying to solve this for the past 6 hours :(
I am importing a data set with importdata(filename)
And its 1 Column with a 1000 rows of numbers
So I have been trying to compute and output the cumulative moving average. eg. [1, 2 , 3 ] would compute into a row of values :
Below is the maths for It.
1. 1/1 = 1
2. 1+2/2 = 1.5
3. 1+2 +3 /3 = 2 etc
This is the code I got so far RA = movmean(A,2); disp('Moving Average of all the data: '); disp(RA);
but it is outputting incorrect values.
preferably im trying to make a function to do the cumulative moving average rather than use built in function
thanks
edit I have added A = importdata(filename);
size(A); NumberOfRows=1000; NumberOfColums=1;
disp(A)
so hopefully it wil be easier now :S

Rena Berman

Rena Berman (view profile)

2017 年 1 月 20 日
(Answers Dev) Restored Question.

サインイン to comment.

2 件の回答

Chris Turnes (view profile)

2016 年 10 月 17 日

If you're trying to do a cumulative moving mean, where each time you move to the next element it is the mean of that element and all elements before it, then instead try:
b = movmean(A, [length(A)-1 0]);
By default, since Endpoints are shrink, this will give you b(1) = A(1), b(2) = 1/2*(A(1) + A(2)), b(3) = 1/3*(A(1) + A(2) + A(3)), etc.

David Goodmanson

David Goodmanson (view profile)

2016 年 10 月 19 日
good one!

サインイン to comment.

2016 年 10 月 14 日

David Goodmanson (view profile)

2016 年 10 月 14 日

movmean doesn't work because it uses a fixed window width and yours increases as you go. The command B = cumsum(A) will give you the moving sum of terms that you need. The vector n = (1:length(A))' will give you the denominator for calculating the average, where ' turns the row vector into a column vector. Then if you divide these two quantities term-by-term with ./ you should get the result.
It's always a good idea to try this on a short vector and view it.

0 件のコメント

サインイン to comment.