Getting indices of most recent set of numbers in an array in Matlab?

Posted by user2539781 on Stack Overflow See other posts from Stack Overflow or by user2539781
Published on 2013-07-01T16:45:52Z Indexed on 2013/07/01 17:06 UTC
Read the original article Hit count: 163

Filed under:

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?

© Stack Overflow or respective owner

Related posts about matlab