How to get sorted index order for an array?

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 日

20 投票

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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2015 年 5 月 19 日

コメント済み:

2022 年 5 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by