cumulative sum of an array
3 次查看(过去 30 天)
显示 更早的评论
Hi, so I have an array,b, I need to find the cumulative sums for every 5 values.
To calculate the cumulative sum S of an array a with 5 values
b=[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0];
a=[1 2 3 4 5];
amean=mean2(a);
S=zeros([1 length(a)]);
S(1)=a(1)-amean
for i=2:5
S(i)=S(i-1)+(a(i)-amean)
end
S=-2 -3 -3 -2 0
The result for b should look like S = -2 -3 -3 -2 0 0 1 3 6 0 -2 -3 -3 -2 0 0 1 3 6 0
2 个评论
采纳的回答
Matt J
2018-8-13
编辑:Matt J
2018-8-13
br=reshape(b,5,[]);
S=reshape( cumsum(br-mean(br)) ,1,[])
1 个评论
James Tursa
2018-8-13
And, for older versions of MATLAB
S = reshape( cumsum(bsxfun(@minus,br,mean(br))) ,1,[])
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!