Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Picking Ones among Zeros
Date: Mon, 6 Jul 2009 19:51:01 +0000 (UTC)
Organization: Xoran Technologies
Lines: 28
Message-ID: <h2tkj5$sv8$1@fred.mathworks.com>
References: <h2oif2$g5h$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246909861 29672 172.30.248.38 (6 Jul 2009 19:51:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 6 Jul 2009 19:51:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:553196


"Frederic Sigoillot" <sigoillot@yahoo.fr> wrote in message <h2oif2$g5h$1@fred.mathworks.com>...
> Hello,
> 
> What would be the simplest way to retrieve the size of connected components (ones) in a binary string?
> 
> as example, I have
> 
> a= [ 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 0 0 0 0]
> 
> I would like to return
> >>b
>       4
>       7
>       2
> 
> There are 4 connected 'Ones' then 7 and then 2 among the zeros.
> 
> Thank you!
> 
> Frederic



Another (similar) idea that's been floated in other threads:

BlockStarts=strfind([0,a],[0,1]);
BlockEnds=strfind([a 0],[1,0]);
Lengths=BlockEnds-BlockStarts+1;