Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg87428] Re: Pattern problem: How to count from a long list of numbers all
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Thu, 10 Apr 2008 02:13:53 -0400 (EDT)
  • Organization: University of Bergen
  • References: <fti404$ok1$1@smc.vnet.net>

Nasser Abbasi wrote:
> 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 the 
> 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...

This is something that patterns are paticularly *not* suited for. 
Patterns don't work for these kinds of things because a pattern will 
always be tested against a single expression at a time.  Of course there 
is __, ___, and .., but these are used as part of a larger pattern, 
which is tested against single expressions only.  (Actualy this is not 
strictly true when matching with Flat, OneIdentity or Orderless 
functions ... I don't understand completely how those work ...)  For 
example, it is not possible to get the __ pattern to match more than one 
expression when used on its own (and not as part of a larger pattern at 
a deeper level).

However, there does exist a way to accomplish this with patterns.  The 
trick is to count in how many different ways can the {___, 3, 3, ___} 
pattern match the entire list:

In[1]:= x = {1, 3, 3, 3, 2, 3, 3, 1, 3, 3}
Out[1]= {1, 3, 3, 3, 2, 3, 3, 1, 3, 3}

In[2]:= ReplaceList[x, {___, 3, 3, ___} -> Null]
Out[2]= {Null, Null, Null, Null}

In[3]:= Length[%]
Out[3]= 4

> 
> 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

{3,3}_ means {3,3} * _

Note that even in a pattern written as x_, it is _ that matches 
something (it can match any single expression), and not x.  x is just a 
name used to refer to the expression that matches _

> 
> 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 are two alternative ways (without patterns):

Count[Partition[x, 2, 1], {3, 3}]

Count[Table[x[[{i, i + 1}]] === {3, 3}, {i, 1, Length[x] - 1}], True]


  • Prev by Date: Re: a workaround for large EPS files from ContourPlot
  • Next by Date: Re: Re:need help
  • Previous by thread: Re: Pattern problem: How to count from a long list of numbers all
  • Next by thread: Re: Pattern problem: How to count from a long list of numbers all