Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Star Strider
Star Strider am 31 Dez. 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

Weitere Antworten (0)

Kategorien

Mehr zu Operating on Diagonal 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!

Translated by