cumsum reset back to zero every 100 times

4 ビュー (過去 30 日間)
Chris Matthews
Chris Matthews 2017 年 8 月 11 日
編集済み: Andrei Bobrov 2017 年 8 月 12 日
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
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
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);

サインインしてコメントする。

採用された回答

José-Luis
José-Luis 2017 年 8 月 11 日
編集済み: José-Luis 2017 年 8 月 11 日
I interpreted "reset cumsum to zero" as restart cumsum from scratch.
data = rand(1,601);
dummy = zeros(100,ceil(size(data,2)./100));
dummy(1:numel(data)) = data;
dummy = cumsum(dummy,1);
plot(data); hold on
data = dummy(1:numel(data));
plot(data);
  2 件のコメント
Jan
Jan 2017 年 8 月 11 日
+1: Clean and efficient.
José-Luis
José-Luis 2017 年 8 月 11 日
Thanks.

サインインしてコメントする。

その他の回答 (3 件)

Jan
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
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
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
Andrei Bobrov 2017 年 8 月 12 日
編集済み: Andrei Bobrov 2017 年 8 月 12 日
Thanks Jan for your comment!
I am fixed my answer.

サインインしてコメントする。


Walter Roberson
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()

カテゴリ

Help Center および File ExchangeVisualize 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!

Translated by