Getting indices of most recent set of numbers in an array in Matlab?
- by user2539781
Let's say I have an array in Matlab:
x = [1 1 1 2 2 2 3 3 3 3 2 2 3 3 3 1 1 1 1 1 2 2 2 2 3 3 3]
I want the indices of the most recent set of consecutive '1's. In this case, I want the answer:
answer = [16 17 18 19 20] % (which should correspond to the last sequence of 1's)
I need an automated way to do this in Matlab, that will work with any size arrays, always giving me the indices for the last set of consecutive 1s.
Any suggestions?