RE: Re: Pattern Matching in Lists
- To: mathgroup at smc.vnet.net
- Subject: [mg35610] RE: [mg35590] Re: Pattern Matching in Lists
- From: "DrBob" <majort at cox-internet.com>
- Date: Mon, 22 Jul 2002 02:11:14 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
Wow, Carl! That's about three times as fast as Selwyn's code!
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])]
trial:=(n=2000000;w = Table[Random[Integer], {n}];
First@Timing[#[w];]/Second&/@{f3,f4}
)
{t1,t2}=Transpose[trial&/@Range[30]];
Mean/@{t1,t2}
r=MeanTest[t1-t2,0,FullReport\[Rule]True]
meanDiff=(FullReport/.r)[[1,1,1]];
Print["% difference = ",meanDiff/Mean[t1]]
{0.560933, 0.2105}
{FullReport -> TableForm[
{{"Mean", "TestStat",
"Distribution"},
{0.35043333333333826,
44.64543969496886,
StudentTDistribution[29]}},
TableHeadings -> {None,
{"Mean", "TestStat",
"Distribution"}}],
OneSidedPValue ->
1.377763589983338*^-28}
% difference = 0.6247325885429081
That's 62% less time spent, with a p-value of about 10^-28.
Bobby
-----Original Message-----
From: Carl K. Woll [mailto:carlw at u.washington.edu]
To: mathgroup at smc.vnet.net
Subject: [mg35610] [mg35590] Re: Pattern Matching in Lists
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
>
>
>
>