How do I get the position of the highest numbers of a vector?

1 view (last 30 days)
Hi everyone,
Does anyone know how to get the position of 4 highest numbers of a vector? For example,
rng(1)
a = randn(10,1)
a =
-0.6490
1.1812
-0.7585
-1.1096
-0.8456
-0.5727
-0.5587
0.1784
-0.1969
0.5864
Clearly the four highest numbers are 1.1812 , 0.5864 , 0.1784 and -0.1969. That correspond to element 2,10,8 and 9, respectively. How can I get this sequence in a vector form, ie 4x1?
Thanks a lot.

Accepted Answer

dpb
dpb on 9 Apr 2017
>> [mx,ix]=sort(a,'descend');
>> N=4;
>> num2str([ix(1:N) mx(1:N) ],'Posn %2d Value %7.4f')
ans =
Posn 2 Value 1.1812
Posn 10 Value 0.5864
Posn 8 Value 0.1784
Posn 9 Value -0.1969
>>

More Answers (0)

Community Treasure Hunt

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

Start Hunting!