How to group by in matlab?

13 visualizaciones (últimos 30 días)
chen xie
chen xie el 8 de Mayo de 2021
Editada: Cris LaPierre el 8 de Mayo de 2021
There is a matrix: [1 3 1;1 3 2;1 3 3;1 3 4;3 1 2;3 1 3]
i want to group by first two columns, and sum the third column.
idealy result is [1 3 10;3 1 5]
  1 comentario
chen xie
chen xie el 8 de Mayo de 2021
The first two columns of matrix are considered indexes. This is only an example, my dataset is very large and don't know which indexes are duplicated,I want to sum the third column with repeated indexes to form the final matrix.

Iniciar sesión para comentar.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 8 de Mayo de 2021
Editada: Cris LaPierre el 8 de Mayo de 2021
Perhaps with groupsummary?
A=[1 3 1;1 3 2;1 3 3;1 3 4;3 1 2;3 1 3];
[B,BG]=groupsummary(A(:,3),{A(:,1),A(:,2)},'sum');
C = [cell2mat(BG),B]
C = 2×3
1 3 10 3 1 5

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by