Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

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

Search the Archive

Re: Pattern Matching in Lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg35586] Re: Pattern Matching in Lists
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sun, 21 Jul 2002 01:01:03 -0400 (EDT)
  • References: <ah5qce$59o$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

[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