Borrar filtros
Borrar filtros

Find indicies of unsorted locations in an array

2 visualizaciones (últimos 30 días)
Erez
Erez el 2 de Abr. de 2019
Comentada: Stephan el 2 de Abr. de 2019
I have a long array, e.g.
A=[ 1 2 5 8 7 9 110 100]
I want to get an array B, which contains the left-index of the positions where the array is not sorted (the array is supposed to be sorted from the lowest value on the left, to the highest value on the right), namely
B=[4,7]
How can I do that without using loops?
Another example, just to make the point clear: I assume that the array is more-or-less already sorted, so AA=[-1 1 3 2 0] does not occur, but AA=[-1 1 3 2 5] does.
The latter will yield: B=[3].
Also AA=[-1 1 8 2 3] does not occur. I assume that if there are non-sorted positions, then by switching the locations of only nearest neighbours (i, i+1) I can make the array sorted again.
Thanks!
  3 comentarios
Erez
Erez el 2 de Abr. de 2019
Hm, right, silly me.. Thanks!
Stephan
Stephan el 2 de Abr. de 2019
B = find(diff(A)<0)

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by