cumsum reset back to zero every 100 times

5 vues (au cours des 30 derniers jours)
Chris Matthews
Chris Matthews le 11 Août 2017
Modifié(e) : Andrei Bobrov le 12 Août 2017
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 commentaires
Walter Roberson
Walter Roberson le 11 Août 2017
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 le 11 Août 2017
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);

Connectez-vous pour commenter.

Réponse acceptée

José-Luis
José-Luis le 11 Août 2017
Modifié(e) : José-Luis le 11 Août 2017
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 commentaires
Jan
Jan le 11 Août 2017
+1: Clean and efficient.
José-Luis
José-Luis le 11 Août 2017
Thanks.

Connectez-vous pour commenter.

Plus de réponses (3)

Jan
Jan le 11 Août 2017
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 le 11 Août 2017
Modifié(e) : Andrei Bobrov le 12 Août 2017
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 commentaires
Jan
Jan le 11 Août 2017
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 le 12 Août 2017
Modifié(e) : Andrei Bobrov le 12 Août 2017
Thanks Jan for your comment!
I am fixed my answer.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 11 Août 2017
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()

Catégories

En savoir plus sur Visualize and Interpret Parallel Link Project Analysis Results dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by