Re: Re: Re: Counting Runs
- To: mathgroup at smc.vnet.net
- Subject: [mg52007] Re: [mg51993] Re: [mg51934] Re: [mg51890] Counting Runs
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sun, 7 Nov 2004 01:03:22 -0500 (EST)
- References: <200411040650.BAA18131@smc.vnet.net> <200411050717.CAA06890@smc.vnet.net> <200411060708.CAA26052@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
But it's very easy to adapt my method to general data: v = Table[Random[Integer,{0,3}],{20}] {0,2,2,1,2,1,1,1,1,1,3,2,3,2,3,3,0,3,3,1} With[{u=Split[v],w=Union[v]},Map[Count[u,{Repeated[#]}]&,w]] {2,3,4,4} Andrzej On 6 Nov 2004, at 16:08, DrBob wrote: > I timed the posted methods except Andrzej's -- it's the only one that > works only for +1/-1 data -- plus a couple of my own that I haven't > posted. David Park's method seems the same as the fastest method, > hanlon3. I modified all methods to return a pair {x, number of runs in > x} for each x in the data. > > Two of Bob Hanlon's methods beat all the rest of us -- but one of his > is the slowest method, too. > > I've posted a notebook at the Run Counts link at: > > http://eclecticdreams.net/DrBob/mathematica.htm > > Bobby > > On Fri, 5 Nov 2004 02:17:54 -0500 (EST), Selwyn Hollis > <sh2.7183 at misspelled.erthlink.net> wrote: > >> 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 >> (edit reply-to to reply) >> >> >> 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 >>> >>> >> >> >> >> > > > > -- > DrBob at bigfoot.com > www.eclecticdreams.net >
- References:
- Counting Runs
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Re: Counting Runs
- From: Selwyn Hollis <sh2.7183@misspelled.erthlink.net>
- Re: Re: Counting Runs
- From: DrBob <drbob@bigfoot.com>
- Counting Runs