finding the max values

9 vues (au cours des 30 derniers jours)
hanan abu elhejaa
hanan abu elhejaa le 20 Jan 2018
if i have an array and a number k how can I find the biggest k number in the array . but the number should be according there positing in the array : arr= [ 1 9 4 7 38 8] k= 4 the ans must be = [ 9 7 38 8]

Réponses (3)

ANKUR KUMAR
ANKUR KUMAR le 20 Jan 2018
Modifié(e) : ANKUR KUMAR le 20 Jan 2018
arr= [ 1 9 4 7 38 8]
k= 4;
[arr1 b]=sort(arr,'descend');
b1=b(1:k)
arr(sort(b1,'ascend'))
  1 commentaire
hanan abu elhejaa
hanan abu elhejaa le 20 Jan 2018
thank you so much :)

Connectez-vous pour commenter.


Star Strider
Star Strider le 20 Jan 2018
See if this does what you want:
k = 4;
arr = [ 1 9 4 7 38 8];
Result = arr(arr > k)
Result =
9 7 38 8
  1 commentaire
Walter Roberson
Walter Roberson le 20 Jan 2018
The question appears to want k to be the number of values to return.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 20 Jan 2018
With R2017b or later use maxk https://www.mathworks.com/help/matlab/ref/maxk.html
To get the elements in vector order use the two output form and ignore the first output and index the original vector at sort() of the second output

Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by