Find a block of ones in a vector

조회 수: 3 (최근 30일)
fcarl
fcarl 2011년 9월 12일
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.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 12일
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
Fangjun Jiang
Fangjun Jiang 2011년 9월 12일
See my update.
Andrei Bobrov
Andrei Bobrov 2011년 9월 12일
ind = strfind([0 a],[0 1])

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

fcarl
fcarl 2011년 9월 12일
I recognized this ;) the function was helpfull :)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by