Re: Counting Runs

• To: mathgroup at smc.vnet.net
• Subject: [mg51934] Re: [mg51890] Counting Runs
• From: Selwyn Hollis <sh2.7183 at misspelled.erthlink.net>
• Date: Fri, 5 Nov 2004 02:17:54 -0500 (EST)
• References: <200411040650.BAA18131@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi Greg,

The following seems to work pretty well:

runscount[lst_?VectorQ] :=
Module[{elems, flips, counts},
elems = Union[lst];
flips = Cases[Partition[lst, 2, 1], {x_, y_} /; x =!= y];
counts = {#, Count[Most[flips], {#, _}]} & /@ elems;
{x1, x2} = Last[flips];
counts /. {{x1, y_} -> {x1, y+1}, {x2, y_} -> {x2, y+1}}]

Example:

Table[Random[Integer, {1, 5}], {20}]
runscount[%]

{2, 2, 3, 1, 3, 2, 2, 3, 1, 1, 2, 3, 1, 1, 3, 1, 1, 2, 2, 2}

{{1, 4}, {2, 4}, {3, 5}}

-----
Selwyn Hollis
http://www.appliedsymbols.com

On Nov 4, 2004, at 1:50 AM, Gregory Lypny wrote:

> Looking for an elegant way to count runs to numbers in a series.
> Suppose I have a list of ones and negative ones such as
> 	v={1,1,1,-1,1,1,1,1,1,-1,-1,-1,-1,1}.
> I'd like to create a function that counts the number of runs of 1s and
> -1s, which in this case is 3 and 2.
>
> 	Greg
>
>

```

• References:
• Prev by Date: Re: Re: Plotting scattergram
• Next by Date: Re: Functions with optional parameters
• Previous by thread: Re: Counting Runs
• Next by thread: Re: Re: Counting Runs