Clear Filters
Clear Filters

How to find rows with maximum number

1 view (last 30 days)
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

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!