Reputation: 539
I have an array something like this:
[0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 0 0 0 1 1 ... ]
I want to find the indices where 0 changes to 1 and 1 changes to 0. So for the following array:
changes = [5 10 14 17 20]
I know how to use find and a vector predicate expression like find(A > 3)
to search for simpler conditions, but this has me stuck. I could write a big loop and do it, but I feel there must be something built-in or otherwise easier to achieve the same thing.
Upvotes: 3
Views: 2392
Reputation: 21532
Dennis K's solution works for your binary data. If you wish to find transitions in general, I recommend Run Length Encoding. I don't think MATLAB has a builtin, but (shameless plug), you can use seqle .
Upvotes: 0
Reputation: 769
A very simple approach which works with all values as start would be:
changes = find(diff(value))+1;
This will also return the expected result changes = [5 10 14 17 20]
.
Upvotes: 5
Reputation: 539
Figured out a solution.
find(circshift(value, [0, 1]) ~= value)
For my application, the array is guaranteed to begin and end with 0s, otherwise it will not work properly.
Upvotes: 3