Cumulative sum with condition
5 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dave B
on 22 Oct 2021
Edited: Dave B
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!