How to get first 3 maximum number in a matrix

6 views (last 30 days)
Suppose I have a matrix m:
m = [7;1;4;4;12;2;6;10;2];
I want to find first 3 maximum in matrix m, means like:
n = [7;12;10]; % sorting is not issue

Accepted Answer

Adam on 4 Nov 2014
temp = sort( m, 'descend' );
n = temp(1:3);
Adam on 5 Nov 2014
[temp, idx] = sort( m, 'descend' );
n = temp(1:3);
idx = idx(1:3);

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 4 Nov 2014

Matt J
Matt J on 5 Nov 2014
Bruno Luong took the trouble to make a fast MEX implementation


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!

Translated by