Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Star Strider
Star Strider el 31 de Dic. de 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

Más respuestas (0)

Categorías

Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by