Efficient way to calculate backwards average
10 ビュー (過去 30 日間)
古いコメントを表示
Dear all,
I'm looking for an efficient way to calculate a backwards moving average, i.e., giving a vector A I want to calculate a vector A2 for which the element i is equal to mean(A(i:end)).
For the moment I am doing it this way:
A=rand(1,1000);
n=length(A);
A2=zeros(1,n)
for i=1:n
A2(i)=mean(A(i:end));
end
Is there any better way?
Thanks
Lorenzo
0 件のコメント
採用された回答
John D'Errico
2014 年 9 月 29 日
編集済み: John D'Errico
2014 年 9 月 29 日
First of all, what you SAY you are doing makes no sense. A is a 1000x1000 matrix, but A2 only a vector. And you have two i for loops, with only one end. And regardless of what size A is, n=size(A) will produce a vector. So 1:n will yield a problem in the for loop.
The code you show will fail in so many ways I won't bother to count. You should provide working code so someone can know what it is you really want!
Assuming that you really wanted to write this where A is a row vector...
Think about what cumsum does. Then, suppose you flipped the data before calling cumsum.
n = length(A);
A2 = fliplr(cumsum(fliplr(A))./(1:n)));
Of course, this is really not a true moving average, since that would involve a moving window of fixed length. But it is what you asked for.
4 件のコメント
John D'Errico
2014 年 10 月 1 日
That will work fine, although a minor optimization would be to use bsxfun to do the divide instead of replicating the vector using repmat.
A2=flipud(bsxfun(@rdivide,cumsum(flipud(A)),(1:rows)'));
その他の回答 (4 件)
José-Luis
2014 年 9 月 29 日
numRows = 100;
numCols = 100;
data = rand(numRows,numCols);
result = flipud(bsxfun(@rdivide,cumsum(flipud(data)),(1:numRows)));
Chad Greene
2014 年 9 月 29 日
This is a very fast moving average calculator. It centers data, so if you use an N-point moving average, after calculating the moving averaged, you could shift by N/2 to get the "backwards" moving average.
1 件のコメント
Image Analyst
2014 年 9 月 29 日
He doesn't want a moving average. His window is not constant length, but gets shorter as the index approaches the end of the array.
SK
2014 年 9 月 29 日
編集済み: SK
2014 年 9 月 29 日
s = sum(A);
n = length(A);
A2 = (s - cumsum(A))/n;
is a little more elegant and I would think faster. But you have to add s/N to the beginning of A2 and remove the 0 at the end of A2.
The last operation (removing the zero) is misleadingly innocent:
A2(end) = [];
But you may soon get to know the consequences of it.
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!