Find indicies of unsorted locations in an array

7 vues (au cours des 30 derniers jours)
Erez
Erez le 2 Avr 2019
Commenté : Stephan le 2 Avr 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 commentaires
Erez
Erez le 2 Avr 2019
Hm, right, silly me.. Thanks!
Stephan
Stephan le 2 Avr 2019
B = find(diff(A)<0)

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by