RE: Re: Pattern Matching in Lists
- To: mathgroup at smc.vnet.net
- Subject: [mg35609] RE: [mg35586] Re: Pattern Matching in Lists
- From: "DrBob" <majort at cox-internet.com>
- Date: Mon, 22 Jul 2002 02:11:12 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
Selwyn's code is almost always a little faster than Allan's (though not much). The difference is around 4% for lists of 2,000,000 entries. Here are results for thirty trials of the two arithmetic methods, with 2 million entries each time, using a paired t-test: Needs["Statistics`HypothesisTests`"] f1[w_]:=Count[Partition[w, 2,1],{1,0}] f2[w_]:=Count[Drop[w,-1] - Drop[w,1],1] f3[w_]:=Count[Drop[w+2RotateRight[w],1],2] trial:=(n=2000000;w = Table[Random[Integer], {n}]; First@Timing[#[w];]/Second&/@{f2,f3} ) {t1, t2} = Transpose[(trial & ) /@ Range[30]]; Mean /@ {t1, t2} r = MeanTest[t1 - t2, 0, FullReport -> True] meanDiff = (FullReport /. r)[[1,1,1]]; Print["% difference = ", meanDiff/Mean[t1]] {0.5864333333333358, 0.5621333333333306} {FullReport -> TableForm[ {{"Mean", "TestStat", "Distribution"}, {0.024300000000005186, 3.3246821117809677, StudentTDistribution[ 29]}}, TableHeadings -> {None, {"Mean", "TestStat", "Distribution"}}], OneSidedPValue -> 0.0012041503843005326} "% difference = "0.0414369351 That's a 4% difference and a p-value of about 0.1%. Bobby Treat -----Original Message----- From: Allan Hayes [mailto:hay at haystack.demon.co.uk] To: mathgroup at smc.vnet.net Subject: [mg35609] [mg35586] 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 > >