finding the max values

9 次查看(过去 30 天)
hanan abu elhejaa
hanan abu elhejaa 2018-1-20
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]

回答(3 个)

ANKUR KUMAR
ANKUR KUMAR 2018-1-20
编辑:ANKUR KUMAR 2018-1-20
arr= [ 1 9 4 7 38 8]
k= 4;
[arr1 b]=sort(arr,'descend');
b1=b(1:k)
arr(sort(b1,'ascend'))

Star Strider
Star Strider 2018-1-20
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 个评论
Walter Roberson
Walter Roberson 2018-1-20
The question appears to want k to be the number of values to return.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2018-1-20
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

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by