@PGrant: please explain the logic of how to calculate Answer: assume that we a stupid and that every step need to be shown to us.
Cumulative sum of groups
7 次查看(过去 30 天)
显示 更早的评论
I need the cumlative sum of the second column of Data grouped by (or reset at each change in) the first column of Data please:
Data = [[1;1;1; 2;2;2; 3;3;3], [1;0;1; 0;1;1; 0;1;0]]
Answer = [Data, [1;1;2; 0;1;2; 0;1;1]]
Data =
1 1
1 0
1 1
2 0
2 1
2 1
3 0
3 1
3 0
Answer =
1 1 1
1 0 1
1 1 2
2 0 0
2 1 1
2 1 2
3 0 0
3 1 1
3 0 1
采纳的回答
更多回答(1 个)
Lola Davidson
2024-6-3
The grouptransform function was introduced in R2018b to help make problems like this a bit simpler. You can avoid the tricky cell array syntax that you need to use accumarray and just do this:
Data(:,3) = grouptransform(Data(:,2),Data(:,1),@cumsum)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!