Re: Pattern Matching in Lists
- To: mathgroup at smc.vnet.net
- Subject: [mg35605] Re: Pattern Matching in Lists
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Mon, 22 Jul 2002 02:11:05 -0400 (EDT)
- References: <001301c23091$d341ef20$9b16989e@machine1>
- Sender: owner-wri-mathgroup at wolfram.com
Allan, One further small tweak. Your refinement: s1[w_] := (Tr[#1] - Tr[#1 Drop[w, 1]] & )[Drop[w, -1]] My tweak is to use BitAnd instead of multiplication: s2[w_] := (Tr[#1] - Tr[BitAnd[#1, Drop[w, 1]]] & )[Drop[w, -1]] Testing: tst = Table[Random[Integer], {10^7}]; In[4]:= s1[tst]//Timing s2[tst]//Timing Out[4]= {0.641 Second, 2498910} Out[5]= {0.5 Second, 2498910} So, BitAnd appears to be slightly faster. Carl Woll Physics Dept U of Washington ----- Original Message ----- From: "Allan Hayes" <hay at haystack.demon.co.uk> To: mathgroup at smc.vnet.net Subject: [mg35605] 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 > > > > > > > > > > > > > > > > > > > > >