How to get sorted index order for an array?

조회 수: 50 (최근 30일)
Pablo
Pablo 2015년 5월 19일
댓글: Anirudha bajaj 2022년 5월 17일
For example, if I use the sort function on the array [14 8 91 19], I will get [8 14 19 91]. But instead I want [2 1 4 3] which gives me the indices of elements in the original array.
Is there any inbuilt function or does the sort function accept any additional argument to accomplish this? Otherwise I have to write my own function.

답변 (1개)

Stephen23
Stephen23 2015년 5월 19일
편집: Stephen23 2015년 5월 19일
You can obtain the output indices directly from sort:
>> [out,idx] = sort([14 8 91 19])
out =
8 14 19 91
idx =
2 1 4 3
See the sort documentation for more useful information.
  댓글 수: 2
Seraphin Kimaryo
Seraphin Kimaryo 2020년 10월 13일
Thanks buddy.
Anirudha bajaj
Anirudha bajaj 2022년 5월 17일
Thank you so much

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by