# some thing wrong with SORT function

13 visualizaciones (últimos 30 días)
tuo el 8 de Mzo. de 2021
Comentada: Stephen23 el 8 de Mzo. de 2021
When I sort using the sort function，the error happened as the picture showed
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Stephen23 el 8 de Mzo. de 2021
"b does not given the correct location"
It gives exactly the location described in the sort documentation, which states "I is the same size as A and describes the arrangement of the elements of A into B along the sorted dimension. For example, if A is a vector, then B = A(I)." Lets try it and see if that is what MATLAB returns:
V = [35,10,16,10,34];
[a,b] = sort(V,'ascend')
a = 1×5
10 10 16 34 35
b = 1×5
2 4 3 5 1
c = V(b)
c = 1×5
10 10 16 34 35
So far you have not given any explanation of what you think the problem is. I will make a wild guess that you want the inverse indices for applying on the LHS, which can be obtained like so:
[~,d] = sort(b)
d = 1×5
5 1 3 2 4
e = nan(size(d));
e(d) = V
e = 1×5
10 10 16 34 35

Iniciar sesión para comentar.

### Respuestas (1)

Mehmed Saad el 8 de Mzo. de 2021
c = [35 10 16 11 34];
Now after sorting
a = [10 11 16 34 35];
what is the position of first index of a in c. it is 2, for 2nd index of a the position is 4 in c and goes on.
so b is
b = [2 4 3 5 1];
it is giving you positions of elements of a in c.
if you write
c(b)
you will get a
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
tuo el 8 de Mzo. de 2021
thank you very much

Iniciar sesión para comentar.

### Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by