Filter löschen
Filter löschen

How to find rows with maximum number

1 Ansicht (letzte 30 Tage)
Maryam Hamrahi
Maryam Hamrahi am 7 Aug. 2016
Kommentiert: Star Strider am 7 Aug. 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

Akzeptierte Antwort

Star Strider
Star Strider am 7 Aug. 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 Kommentare
Maryam Hamrahi
Maryam Hamrahi am 7 Aug. 2016
Sorry, it was my mistake. I have to correct it myself. I am thankful for your help.
Star Strider
Star Strider am 7 Aug. 2016
My pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by