Borrar filtros
Borrar filtros

How to find rows with maximum number

1 visualización (últimos 30 días)
Maryam Hamrahi
Maryam Hamrahi el 7 de Ag. de 2016
Comentada: Star Strider el 7 de Ag. de 2016
I have a matrix with n rows and 1 column. I would like to find rows which has the maximum number of rows. Then, replace zero in the other rows.
For instance: I have matrix A and I would like to produce matrix B.
A=
5
2
2
4
3
2
B=
0
2
2
0
0
2

Respuesta aceptada

Star Strider
Star Strider el 7 de Ag. de 2016
This works:
A = [5
2
2
4
3
2];
[Au,ia,ic] = unique(A, 'stable');
h = accumarray(ic, 1);
B = A;
B(ic~=Au(h==max(h))) = 0
B =
0
2
2
0
0
2
  6 comentarios
Maryam Hamrahi
Maryam Hamrahi el 7 de Ag. de 2016
Sorry, it was my mistake. I have to correct it myself. I am thankful for your help.
Star Strider
Star Strider el 7 de Ag. de 2016
My pleasure.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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