Alberto CR
Alberto CR

Reputation: 3

Matlab - Take first non-zero position in logical array

My problem is the following: is there an easy way to take the position of the the first non zero element in a non zero streak in a logical array? There may be several 'ones' streaks with 'zeroes' streaks in between.

Example Input:

1 1 1 1 0 0 0 1 1 1 0 0 1 1 1

Example output:

1 0 0 0 0 0 0 1 0 0 0 0 1 0 0

Thank you!

Upvotes: 0

Views: 61

Answers (1)

MrAzzaman
MrAzzaman

Reputation: 4768

You should be able to do this using the diff command:

output = diff([0,input])>0;

diff takes the difference between adjacent values in a MATLAB array. You have to append a leading zero to ensure that a leading one is caught, should it exist.

Upvotes: 3

Related Questions