Find a block of ones in a vector

Hi,
I have a simple question but no matlab command for this :). I have a vector containing zeros and ones. I want to find every beginning of a block containing only of ones. For example: 000110011100. I want to get 4 and 8 as the indices indicating the beginning of a block with only ones.
Thank you for your efforts.

 采纳的回答

a=[0 0 0 1 1 0 0 1 1 1 0 0]
ind=find(diff(a)==1)+1
Not too fast. This won't work if a starts with 1. Use this instead
a=[1 1 0 0 0 1 1 0 0 1 1 1 0 0]
ind=find(diff([0 a])==1)

3 个评论

Great :) diff is exactly which i was searching for :)
ind = strfind([0 a],[0 1])

请先登录,再进行评论。

更多回答(1 个)

fcarl
fcarl 2011-9-12

0 个投票

I recognized this ;) the function was helpfull :)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by