MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Counting Runs

  • To: mathgroup at smc.vnet.net
  • Subject: [mg51959] Re: Counting Runs
  • From: Peter Pein <petsie at arcor.de>
  • Date: Fri, 5 Nov 2004 02:19:41 -0500 (EST)
  • References: <cmclmu$i99$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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
> 

Simply split and count:

countruns[data_, items_] := Module[{splitlist = Split[data]},
     Count[splitlist, {x__} /; x == #] & /@ items]

v = {1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1};

In[3]:= countruns[v, {1, -1}]
Out[3]= {3, 2}


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