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일

1 개 추천

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일

0 개 추천

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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

질문:

2018년 1월 20일

답변:

2018년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by