Cumulative sum with condition
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ravi Kumar
am 22 Okt. 2021
Bearbeitet: Dave B
am 22 Okt. 2021
I want to do cumulative sum of column 3/4 with condition in coulmn 5/6 .
example:
i.e. -0.0700+0
than -0.9800 + (-0.9800+0.4950) + (-0.9800+0.4950+0.1680)+.....so on till 0.2070
than it should start summing 3: 3.3590 + (3.3590 + 1.7470) + .....
0 Kommentare
Akzeptierte Antwort
Dave B
am 22 Okt. 2021
Bearbeitet: Dave B
am 22 Okt. 2021
x=[rand(10,1) randi(3,10,1)];
% this is the sum for groups 1,2,3
[groupsums,groups] = groupsummary(x(:,1),x(:,2),'sum')
% this is what you described, the sum of the cumulative sum
[groupsums,groups]=groupsummary(x(:,1),x(:,2),@(x)sum(cumsum(x)))
% here's just the cumsum:
x=sortrows(x,2); % sorting is not necessary, just makes it easier to visualize
groupcumsums=grouptransform(x(:,1),x(:,2),@(x)cumsum(x));
[x groupcumsums]
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical 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!