Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!f8g2000vbl.googlegroups.com!not-for-mail
From: Lucy <comtech.usa@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to generate the runs in a vector fast?
Date: Wed, 16 Jun 2010 07:23:10 -0700 (PDT)
Organization: http://groups.google.com
Lines: 69
Message-ID: <029a84fb-2ab8-415c-8904-72c2c3d8b055@f8g2000vbl.googlegroups.com>
References: <75f39778-f788-46b7-8927-1f381338e9d1@f16g2000vbl.googlegroups.com> 
	<hv91nc$q12$1@fred.mathworks.com>
NNTP-Posting-Host: 199.89.103.11
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1276698190 31490 127.0.0.1 (16 Jun 2010 14:23:10 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 16 Jun 2010 14:23:10 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: f8g2000vbl.googlegroups.com; posting-host=199.89.103.11; 
	posting-account=7r1RcgoAAACkWFPP9rLUrcp40XUTzzvu
User-Agent: G2/1.0
X-HTTP-Via: 1.1 piinbcgw02
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) 
	Gecko/2010041205 Firefox/3.6.3ms1,gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:645375

On Jun 15, 7:18 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Lucy <comtech....@gmail.com> wrote in message <75f39778-f788-46b7-8927-1f381338e...@f16g2000vbl.googlegroups.com>...
> > Hi all,
>
> > I hvae a vector of 1, 0, -1s. I want to find the histogram of the
> > number of consecutive numbers, ie the histogram of the runs.
>
> > For example, how many two 1s, three 1s, four 1s in a row, and those of
> > -1s, etc.
>
> > How to do this fast? Hopefully without for loops?
>
> > Thx a lot
>
>   Let v be a row vector.
>
>  p = diff(find([true,diff(v)~=0,true]));
>  b = histc(p,1:max(p));
>
> Roger Stafford

Thanks Roger but, ...

>> v=[1 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1];

 p = diff(find([true,diff(v)~=0,true]));
 b = histc(p,1:max(p));
>> b

b =

     2     2     2     1

That's not right...

>> v=[1 1 1 0 0 -1 -1 0 -1 1 1 1 1 0 0 -1 0 1 0 1 1];

 p = diff(find([true,diff(v)~=0,true]));
 b = histc(p,1:max(p));
>>
>>
>> p

p =

     3     2     2     1     1     4     2     1     1     1     1
2

>> b

b =

     6     4     1     1

--------------------

I have to separate the runs of 1s and 0s and -1s...

ie. for 1s, I generate a histogram,

for 0s, I generate another historgram

for -1s, one more...

How to do that?

Thank you!