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

1 vue (au cours des 30 derniers jours)
gsourop
gsourop le 9 Avr 2017
Commenté : gsourop le 9 Avr 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.

Réponse acceptée

dpb
dpb le 9 Avr 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
>>

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by