Re: Counting Runs

*To*: mathgroup at smc.vnet.net*Subject*: [mg51917] Re: [mg51890] Counting Runs*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Fri, 5 Nov 2004 02:17:09 -0500 (EST)*References*: <200411040650.BAA18131@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 4 Nov 2004, at 15:50, 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 > > Here is one way: v = {1, 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, -1, 1}; With[{u=Split[v]},{Count[u,{Repeated[1]}],Count[u,{Repeated[-1]}]}] {3,2} Andrzej Kozlowski Chiba, Japan http://www.akikoz.net/~andrzej/ http://www.mimuw.edu.pl/~akoz/

**References**:**Counting Runs***From:*Gregory Lypny <gregory.lypny@videotron.ca>