cumsum reset back to zero every 100 times
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chris Matthews
el 11 de Ag. de 2017
Editada: Andrei Bobrov
el 12 de Ag. de 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 comentarios
Walter Roberson
el 11 de Ag. de 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
el 11 de Ag. de 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);
Respuesta aceptada
José-Luis
el 11 de Ag. de 2017
Editada: José-Luis
el 11 de Ag. de 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 comentarios
Más respuestas (3)
Jan
el 11 de Ag. de 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
el 11 de Ag. de 2017
Editada: Andrei Bobrov
el 12 de Ag. de 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 comentarios
Jan
el 11 de Ag. de 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
el 12 de Ag. de 2017
Editada: Andrei Bobrov
el 12 de Ag. de 2017
Thanks Jan for your comment!
I am fixed my answer.
Walter Roberson
el 11 de Ag. de 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()
0 comentarios
Ver también
Categorías
Más información sobre Multirate Signal Processing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!