[a, sort_index]=sort([100;20;30;40])
The sort_index should return
4
1
2
3
But it does not.

 採用された回答

Walter Roberson
Walter Roberson 2019 年 9 月 19 日

1 投票

>> [a, sort_index]=sort([100;20;30;40])
a =
20
30
40
100
sort_index =
2
3
4
1
This output is correct. It tells you that a(1) == 20 came from element #2 of the input, and that a(2) == 30 came from element 3 in the input, and that a(3) == 40 came from element 4 of the input, and that a(4) == 100 came from element 1 of the input.
To get the output you want, use
b(sort_index) = 1:length(sort_index);

4 件のコメント

Franziska Höppe
Franziska Höppe 2021 年 4 月 12 日
It does not work right at mine, see:
test=[1 5;6 2;1 2]
[testgeo,testidx]=sort(test,1)
testsort=zeros(3,2);
for i=1:3
for j=1:2
testsort(testidx(i,j),j)=test(i,j)
end
end
Why testidx look like:
1 2
3 3
2 1
it has to look like (1 and two could switch place), the 3 (5) gets the worng play
1 3
3 2
2 1
I do not understand why?
I tried it on a large sample and it does not work neither..
Could you help me please? :-)
Thank you in advance!
Walter Roberson
Walter Roberson 2021 年 4 月 13 日
test=[1 5;6 2;1 2]
test = 3×2
1 5 6 2 1 2
[testgeo,testidx]=sort(test,1)
testgeo = 3×2
1 2 1 2 6 5
testidx = 3×2
1 2 3 3 2 1
test(testidx(:,2),2)
ans = 3×1
2 2 5
test([3 2 1],2)
ans = 3×1
2 2 5
This tells us that for the second column, [2 3 1] and [3 2 1] lead to the same output for sorting purposes, and so both are correct.
When there are multiple items with the same value, there is the question of which order sort algorithms should output. Some algorithms output the items in the order they happen to encounter them during sorting -- and especially with parallel sorts, or sort/merge algorithms, that can lead to later copies of the identical values happening to get sorted before the earlier copies of the value. Other algorithms take the time to order identical values according to the original order; those are generally referred to as "stable" algorithms. Algorithms that are not "stable" have the issue that if you sort an already sorted array, then the sort order might not be just the indices, but that never happens for "stable" sort algorithms.
(You can see "stable" as an option for unique() where it has a slightly different meaning https://www.mathworks.com/help/matlab/ref/double.unique.html#bs_6vpd-1-setOrder )
Even in algorithms that are not "stable", there is no reason why [3 2 1] would necessarily have to be output instead of [2 3 1] for the indices: it could just happen to return [2 3 1] instead.
Franziska Höppe
Franziska Höppe 2021 年 4 月 13 日
Thank you for you detailed answer! :-)
The issue is if you let run the loop, you get a different result. You get:
2
5
2
Do you know why?
Walter Roberson
Walter Roberson 2021 年 4 月 14 日
I never got that output; which code were you using, and which MATLAB version?

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

その他の回答 (1 件)

James Tursa
James Tursa 2019 年 9 月 19 日

2 投票

The sort index gives the location in the original array of the sorted values. I.e., the sort results "a" are "original_array(sort_index)"
>> x = [100;20;30;40];
>> [a,sort_index] = sort(x)
a =
20
30
40
100
sort_index =
2
3
4
1
>> x(sort_index)
ans =
20
30
40
100
>> isequal(x(sort_index),a)
ans =
1

1 件のコメント

Rainer Ng
Rainer Ng 2019 年 9 月 19 日
I apologize. I should have understood this. Thank you.

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

カテゴリ

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

製品

リリース

R2018b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by