create a new array with group sum

4 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jul 2023
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

More Answers (1)

Paul
Paul on 28 Jul 2023
Check out splitapply

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!