Find indicies of unsorted locations in an array

조회 수: 10 (최근 30일)
Erez
Erez 2019년 4월 2일
댓글: Stephan 2019년 4월 2일
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
Erez
Erez 2019년 4월 2일
Hm, right, silly me.. Thanks!
Stephan
Stephan 2019년 4월 2일
B = find(diff(A)<0)

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by