cumsum reset back to zero every 100 times
3 次查看(过去 30 天)
显示 更早的评论
I have a matrix R = [1x601] size. I want to use cumsum to get an accumulative value but reset cumsum back to zero each 100 positions.
Then I want to plot the result, so there should be 6 times where the plot goes back to zero on the y axis.
Is this possible? Please let me know if you don't understand what I'm trying to achieve, and thanks!! Have a good day, Chris
2 个评论
Walter Roberson
2017-8-11
It is positions corresponding to R(100:100:end) that should be set to 0? Is it positions corresponding to R(1:100:end) that should be set to 0? Is it positions corresponding to R(101:100:end) that should be set to 0? Your vector is length 601, so if you choose 100:100:end then position corresponding to 600 would be set to 0, leaving position corresponding to 601 to accumulate to its own value: is that what you would like?
Akira Agata
2017-8-11
The following code works. I would recommend checking your data and/or code again, or upload your data here in .mat file.
% 1x601 sample numeric array
R = rand(1,601);
output = cumsum(R);
采纳的回答
更多回答(3 个)
Jan
2017-8-11
s = cumsum(x);
d = [0, s(101:100:numel(x))];
s = s - d(ceil((1:numel(x)) / 100));
Now s(101) is 0, which might match your "reset to zero", but usually there is no zero in a cumulative sum of positive non-zero elements. See Walter's question for clarification.
Andrei Bobrov
2017-8-11
编辑:Andrei Bobrov
2017-8-12
with "zeros"
m = 100;
n = numel(R);
R1 = R(:);
R1(m+1:m:n) = 0;
d = accumarray(ceil((1:n)'/m),R1);
R1(m+1:m:n) = -d(1:end-1);
out = cumsum(R1);
without "zeros"
m = 100;
n = numel(R);
R1 = R(:);
d = accumarray(ceil((1:n)'/m),R1);
R1(m+1:m:n) = R1(m+1:m:n) - d(1:end-1);
out = cumsum(R1);
2 个评论
Jan
2017-8-11
I get an error cause by the auto-expanding in
R1(101:100:numel(R)) = R1(101:100:numel(R)) - d(1:end-1);
The right size is a matrix. d(1:end-1).' fixes the problem. But then neither out(100) not out(101) is zero. This is logical for a cumulative sum, but the author asked for "reset to zero".
Andrei Bobrov
2017-8-12
编辑:Andrei Bobrov
2017-8-12
Thanks Jan for your comment!
I am fixed my answer.
Walter Roberson
2017-8-11
If you have the signal processing toolbox, use buffer() to put the signal into columns of appropriate length, zero or delete the appropriate row, cumsum()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Visualize and Interpret Parallel Link Project Analysis Results 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!