sum some values of a column based on an other column
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Antonino Lucifora
am 13 Aug. 2020
Bearbeitet: hosein Javan
am 13 Aug. 2020
hi guys! i have two columns and i want to sum the second columns based on values of the first column. for example, the two columns are:
3 0.287000000000000
3 0.303000000000000
3 0.309000000000000
3 0.287000000000000
2 0.732000000000000
1 0.3380000000
the matrix is 8784*2. so, i want to sum the second column in the three different cases: when the first column is 1,sum all the values corresponding to 1, when 2 and 3 i want to do the same. how can i do? someone can help me?
0 Kommentare
Akzeptierte Antwort
hosein Javan
am 13 Aug. 2020
Bearbeitet: hosein Javan
am 13 Aug. 2020
% A is the matrix sized: 8784*2
idx1 = A(:,1)==1;
idx2 = A(:,1)==2;
idx3 = A(:,1)==3;
data = A(:,2);
sum(data(idx1)) % sum of values in the second column correspond to 1
sum(data(idx2)) % sum of values in the second column correspond to 2
sum(data(idx3)) % sum of values in the second column correspond to 3
0 Kommentare
Weitere Antworten (1)
Alan Stevens
am 13 Aug. 2020
If data is in M, then
C1 = sum(M(M(:,1)==1,2));
C2 = sum(M(M(:,1)==2,2));
C3 = sum(M(M(:,1)==3,2));
is one possibility.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!