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

1 Ansicht (letzte 30 Tage)
gsourop
gsourop am 9 Apr. 2017
Kommentiert: gsourop am 9 Apr. 2017
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.

Akzeptierte Antwort

dpb
dpb am 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
>>

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown 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