Azzera filtri
Azzera filtri

Removing of duplicates from first column and summing of elements in second column

3 visualizzazioni (ultimi 30 giorni)
Dear All,
I need your help as I am not sure how to solve this problem. Namely, from a matrix with N rows and 2 columns, for example:
X =
1 2
2 3
3 4
4 1
4 3
4 2
5 5
6 8
6 1
I want to get new matrix where in the first column will be shown only unique values, and in the second column (in those rows where were duplicates in the first column) will be shown sum of values of second column.
Result:
Y =
1 2
2 3
3 4
4 6
5 5
6 9
I hope I was clear enough.
Thank you in advance.

Risposta accettata

Star Strider
Star Strider il 31 Dic 2017
This works:
[Xu1,~,Ic2] = unique(X(:,1), 'stable');
Tally = accumarray(Ic2, X(:,2));
Y = [Xu1 Tally]
Y =
1 2
2 3
3 4
4 6
5 5
6 9

Più risposte (0)

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by