Cumsum range and values
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to write a loop where each row of the output matrix represents a cumulative sum of the elements of the input matrix, meanwhile adding restrictions ( I only want to the cumulative sum to operate up to the matrix cell/ element that is not zero, or in other words I want to stop the cumsum operation right before the cell value of that particular row is equal to zero) Example: Input row vector A= [1 1 1 2 3 4 5 1 2 1 5 4 1 0 0 0 0 0 0 0 0 0]; so basically I want to get a new vector that operates the cumsum(A) up to the 13th element, which is equal to1) and the rest of the entries need to remain as zeros. can anyone point me in the right direction or maybe suggest a more efficient way of doing this because my actual matrix is (14680*101) dimension.
0 Kommentare
Antworten (1)
Andrei Bobrov
am 15 Dez. 2015
Bearbeitet: Andrei Bobrov
am 15 Dez. 2015
cumsum(A).*(A ~= 0)
or
A = [1 1 1 2 3 4 5 1 2 1 5 4 1 0 0 0 56 4 8 0 0 0]
k = cumsum(A);
cc = A;
t = A ~= 0;
x = strfind(t,[1 ,0]) + 1;
v = conv(k(x),[-1 1]);
cc(x) = v(1:end-1);
out = cumsum(cc)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Linear Algebra finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!