[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!
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 Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2019년 9월 19일

댓글:

2021년 4월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by