Re: Pattern problem: How to count from a long list of numbers all

*To*: mathgroup at smc.vnet.net*Subject*: [mg87611] Re: Pattern problem: How to count from a long list of numbers all*From*: samuel.blake at maths.monash.edu.au*Date*: Mon, 14 Apr 2008 05:45:13 -0400 (EDT)*References*: <ftsd3f$bct$1@smc.vnet.net>

On Apr 13, 5:35 pm, "Dana DeLouis" <dana.... at gmail.com> wrote: > > x = {1, 3, 3, 3, 2, 3, 3, 1, 3, 3} > > And I want to count how many say a 3 followed immediately by 3. > > ...1 followed by a 3, there will be 2 > > Just to be different for this particular problem... > > x = {1, 3, 3, 3, 2, 3, 3, 1, 3, 3}; > > Count[ListConvolve[{3, -3}, x], 0] > 4 > > Count[ListConvolve[{1, -3}, x], 0] > 2 > > -- > HTH :>) > Dana DeLouis > > "Nasser Abbasi" <n... at 12000.org> wrote in message > > news:fti404$ok1$1 at smc.vnet.net... > > > Hello; > > > I think using Pattern is my weakest point in Mathematica. > > > I have this list, say this: (it is all a list of integers, no real > numbers). > > > x = {1, 3, 3, 3, 2, 3, 3, 1, 3, 3} > > > And I want to count how many say a 3 followed immediately by 3. So in th= e > > above list, there will be 4 such occurrences. And if I want to count how= > > many 1 followed by a 3, there will be 2 such cases, etc... > > > I tried Count[] but I do not know how to set the pattern for "3 followed= > by > > a comma followed by 3" or just "3 followed immediately by 3". > > > I tried few things, such as the following > > > In[68]:= Count[x, {3, 3}_] > > Out[68]= 0 > > > Also tried Cases, but again, I am not to good with Patterns, so not sure= > how > > to set this up at this moment. > > > Any ideas will be appreciated. > > > Nasser > > I really need to sit down and study Patterns in Mathematica really well > one > > day :) Here's two other ways, one is slightly faster and one slower: In[20]:= xx = RandomInteger[{1, 5}, 1000000]; In[21]:= Count[Differences[xx], 0] // Timing Out[21]= {0.145709, 200402} In[22]:= Count[ListConvolve[{-3, 3}, xx], 0] // Timing Out[22]= {0.214594, 200402} In[23]:= Timing[Length[xx] - Length[Split[xx]]] Out[23]= {0.322837, 200402} Sam