Cumulative sum with condition
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Dave B
le 22 Oct 2021
Modifié(e) : Dave B
le 22 Oct 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical 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!