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.

采纳的回答

Walter Roberson
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]
results = 2×2
1.0e+00 * 1690492071 2.6 1690492076 3

更多回答(1 个)

Paul
Paul 2023-7-28
Check out splitapply

类别

Help CenterFile Exchange 中查找有关 Preprocessing Data 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by