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.
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].
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With