Cumulative sum with condition
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Dave B
il 22 Ott 2021
Modificato: Dave B
il 22 Ott 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!