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

 Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Sept. de 2019

1 voto

>> [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 comentarios

Franziska Höppe
Franziska Höppe el 12 de Abr. de 2021
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 el 13 de Abr. de 2021
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 el 14 de Abr. de 2021
I never got that output; which code were you using, and which MATLAB version?

Iniciar sesión para comentar.

Más respuestas (1)

James Tursa
James Tursa el 19 de Sept. de 2019

2 votos

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 comentario

Rainer Ng
Rainer Ng el 19 de Sept. de 2019
I apologize. I should have understood this. Thank you.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 19 de Sept. de 2019

Comentada:

el 14 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by