create a new array with group sum
4 次查看(过去 30 天)
显示 更早的评论
I have the following data.
1690492071 238581 1.3
1690492071 238582 1.0
1690492071 238580 0.3
1690492071 238576 0.0
1690492071 238577 0.0
1690492071 238579 0.0
1690492071 238583 0.0
1690492071 238584 0.0
1690492071 238585 0.0
1690492071 238586 0.0
1690492076 238581 1.7
1690492076 238582 1.3
1690492076 238576 0.0
1690492076 238577 0.0
1690492076 238579 0.0
1690492076 238580 0.0
1690492076 238583 0.0
1690492076 238584 0.0
1690492076 238585 0.0
1690492076 238586 0.0
...
The first column is the group id and the third column is the value I want to sum for each group. The result should be:
1690492071 2.6
1690492076 3.0
...
I have been struggling for hours, really appreciate help given.
0 个评论
采纳的回答
Walter Roberson
2023-7-28
data = [ ...
1690492071 238581 1.3
1690492071 238582 1.0
1690492071 238580 0.3
1690492071 238576 0.0
1690492071 238577 0.0
1690492071 238579 0.0
1690492071 238583 0.0
1690492071 238584 0.0
1690492071 238585 0.0
1690492071 238586 0.0
1690492076 238581 1.7
1690492076 238582 1.3
1690492076 238576 0.0
1690492076 238577 0.0
1690492076 238579 0.0
1690492076 238580 0.0
1690492076 238583 0.0
1690492076 238584 0.0
1690492076 238585 0.0
1690492076 238586 0.0];
[totals, groups] = groupsummary(data(:,3), data(:,1), "sum");
format long g
results = [groups, totals]
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!