Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to generate the runs in a vector fast?

Subject: How to generate the runs in a vector fast?

From: Lucy

Date: 15 Jun, 2010 22:37:02

Message: 1 of 5

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

Subject: How to generate the runs in a vector fast?

From: Roger Stafford

Date: 15 Jun, 2010 23:18:04

Message: 2 of 5

Lucy <comtech.usa@gmail.com> wrote in message <75f39778-f788-46b7-8927-1f381338e9d1@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

Subject: How to generate the runs in a vector fast?

From: Lucy

Date: 16 Jun, 2010 14:23:10

Message: 3 of 5

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!

Subject: How to generate the runs in a vector fast?

From: us

Date: 16 Jun, 2010 14:33:04

Message: 4 of 5

Lucy <comtech.usa@gmail.com> wrote in message <75f39778-f788-46b7-8927-1f381338e9d1@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

this little FEX utility may be helpful

http://www.mathworks.com/matlabcentral/fileexchange/6436

us

Subject: How to generate the runs in a vector fast?

From: Roger Stafford

Date: 16 Jun, 2010 18:43:06

Message: 5 of 5

Lucy <comtech.usa@gmail.com> wrote in message <029a84fb-2ab8-415c-8904-72c2c3d8b055@f8g2000vbl.googlegroups.com>...
> ......
> Thanks Roger but, ...
> ......

  I have modified the code I sent you to allow for a separation of the "1s and 0s and -1s". However it isn't clear what form you want this in, so I have generated three separate histogram bin lists, b1, b2, and b3, for you to use. To get a histogram plot of these use the 'bar' function.

 f = find([true,diff(v)~=0,true]);
 p = diff(f);
 v2 = v(f(1:end-1));
 p1 = p(v2==-1);
 p2 = p(v2== 0);
 p3 = p(v2== 1);
 b1 = histc(p1,1:max(p1)); % <-- Histogram for -1's
 b2 = histc(p2,1:max(p2)); % <-- Histogram for 0's
 b3 = histc(p3,1:max(p3)); % <-- Histogram for 1's

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us