Aggregating numbers based on first column index
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mido
el 4 de Nov. de 2016
Comentada: Mido
el 5 de Nov. de 2016
Hi
I have a matrix as follows
x=[1 3;
1 4;
1 6;
2 5;
2 7;
3 4;
3 11;
3 1;
3 2]
I want to write a code or a loop that aggregates the numbers in the second column for each different number in the first column.
The result should be
y= [1 13;
2 12;
3 18]
0 comentarios
Respuesta aceptada
Walter Roberson
el 4 de Nov. de 2016
[unique_keys, ~, uq_idx] = unique(x(:,1));
totaled_data = accumarray(uq_idx, x(:,2));
y = [unique_keys, totaled_data];
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing 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!