How to group arrays in matrix

5 visualizaciones (últimos 30 días)
Amy Xu
Amy Xu el 10 de Abr. de 2017
Comentada: Stephen23 el 10 de Abr. de 2017
Matrix A as follows:
A = [
1
1
1
2
2
4
4
7
8
8
9
];
I want to group by matrix A as follows:
B = [
1 3
2 2
4 2
7 1
8 2
9 1
];
  1 comentario
Stephen23
Stephen23 el 10 de Abr. de 2017
The old fashioned way:
>> U = unique(A);
>> [U,hist(A,U).']
ans =
1 3
2 2
4 2
7 1
8 2
9 1

Iniciar sesión para comentar.

Respuestas (2)

Thorsten
Thorsten el 10 de Abr. de 2017
Editada: Thorsten el 10 de Abr. de 2017
If the second row is the number of occurrences then you can use
[a, ~, c] = unique(A);
B = [a, accumarray(c, 1)];

Guillaume
Guillaume el 10 de Abr. de 2017
A = [1 1 1 2 2 4 4 7 8 8 9]'
B = unique(A);
B = [B, histcounts(A, [B; Inf]).']

Categorías

Más información sobre Matrices and Arrays 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