create a new array with group sum

4 Ansichten (letzte 30 Tage)
Igenyar
Igenyar am 28 Jul. 2023
Kommentiert: Igenyar am 28 Jul. 2023
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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

Weitere Antworten (1)

Paul
Paul am 28 Jul. 2023
Check out splitapply

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by