RE: Re: Pattern Matching in Lists
- To: mathgroup at smc.vnet.net
- Subject: [mg35617] RE: [mg35596] Re: Pattern Matching in Lists
- From: "DrBob" <majort at cox-internet.com>
- Date: Tue, 23 Jul 2002 01:51:16 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
f6 appears to be about twice as fast as f4: 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] f4[w_] := Tr[Drop[w, -1](Drop[w, -1] - Drop[w, 1])] f5[w_] := (Tr[w] - Tr[BitAnd[w, RotateLeft[w]]]) + If[w[[ 1]] == 0 && w[[-1]] == 1, -1, 0] f6[w_] := (Tr[#] - Tr[# Drop[w, 1]]) &[Drop[w, -1]] trial := (n = 2000000; w = Table[Random[Integer], {n}]; First@Timing[#[w];]/Second & /@ {f4, f6} ) {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.21726666666666658, 0.11146666666666712} {FullReport -> TableForm[ {{"Mean", "TestStat", "Distribution"}, {0.10579999999999945, 15.693202721924115, StudentTDistribution[29]}}, TableHeadings -> {None, {"Mean", "TestStat", "Distribution"}}], OneSidedPValue -> 5.207406739210963*^-16} % difference = 0.4869591899 f5 is still about 1.5 times as fast as f6, though: trial := (n = 2000000; w = Table[Random[Integer], {n}]; First@Timing[#[w];]/Second & /@ {f6, f5} ) (same test code as above) {0.09896666666666647, 0.0691333333333335} {FullReport -> TableForm[ {{"Mean", "TestStat", "Distribution"}, {0.029833333333332965, 5.308061210842354, StudentTDistribution[29]}}, TableHeadings -> {None, {"Mean", "TestStat", "Distribution"}}], OneSidedPValue -> 5.387889994701921*^-6} % difference = 0.3014482990 Bobby Treat -----Original Message----- From: Allan Hayes [mailto:hay at haystack.demon.co.uk] To: mathgroup at smc.vnet.net Subject: [mg35617] [mg35596] Re: Pattern Matching in Lists Carl, Pushing your idea a bit further and avoiding one Drop and a subtraction of lists: w=Table[Random[Integer],{1000000}]; Tr[Drop[w,-1](Drop[w,-1]-Drop[w,1])]//Timing {7.85 Second,249850} (Tr[#]-Tr[# Drop[w,1]])&[Drop[w,-1] ]//Timing {2.75 Second,249850} -- 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 "Carl K. Woll" <carlw at u.washington.edu> wrote in message news:ahdfd2$i3i$1 at smc.vnet.net... > Anthony and newsgroup, > > I thought of another method of solving this problem which is 5 or 6 times > faster than my previous version. Here is a test case. > > tst=Table[Random[Integer],{1000000}]; > > My first solution was > > In[16]:= > Count[Partition[tst,2,1],{1,0}]//Timing > Out[16]= > {0.704 Second, 249722} > > My second solution is > > In[17]:= > Tr[Drop[tst,-1](Drop[tst,-1]-Drop[tst,1])]//Timing > Out[17]= > {0.125 Second, 249722} > > Carl Woll > Physics Dept > U of Washington > > "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 > > > > > > > > > > >