MathGroup Archive 2002

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

Search the Archive

Re: Pattern Matching in Lists


[second posting in view of reported technical problem]

Anthony,
Take
    w = Table[Random[Integer], {200000}

My first thought was, and several posts used this,

    Count[Partition[w, 2,1],{1,0}]//Timing

        {3.24 Second,49851}

Later it occured to me to use arithmetic, which turned out to be twice as
fast:

     Count[ Drop[w,-1] - Drop[w,1],1]//Timing

        {1.49 Second,49851}

This is close to Selwyn Hollis's code

    Count[Drop[w+2RotateRight[w],1],2]//Timing

        {1.6 Second,49851}

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Anthony Mendes" <amendes at zeno.ucsd.edu> wrote in message
news:ah5qce$59o$1 at smc.vnet.net...
> Hello,
>
> Suppose w={1,1,1,0,0,1,0,1,0,0,1,0,0}.
>
> How can I count the number of occurrences of a 1 in w immediately
> followed by a 0 in w?
>
> I have tried every incarnation of Count[] I can think of; for example,
>
> Count[w,{___,1,0,___}]
>
> does not seem to work.  In general, how can I count the number of
> occurrences of a 1 followed by a 0 in a list of 1's and 0's?  Thank you!
>
>
> --
> Tony
> _____________________
> amendes at math.ucsd.edu
>
>







  • Prev by Date: Re: Pattern Matching in Lists
  • Next by Date: RE: data structure for sets
  • Previous by thread: Re: Pattern Matching in Lists
  • Next by thread: Re: Pattern Matching in Lists