cumsum reset back to zero every 100 times
조회 수: 4 (최근 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!