Re: Pattern problem: How to count from a long list of numbers all occurrences of 2 numbers next to each others?
- To: mathgroup at smc.vnet.net
- Subject: [mg87411] Re: [mg87393] Pattern problem: How to count from a long list of numbers all occurrences of 2 numbers next to each others?
- From: "W Craig Carter" <ccarter at mit.edu>
- Date: Thu, 10 Apr 2008 02:10:41 -0400 (EDT)
- References: <200804090956.FAA25006@smc.vnet.net>
Hello Nasser, This is not the most elegant solution, but it works: Count[Thread[List[x, RotateLeft[x]]], {1, 3}] which would be convenient to write as a function CountMySeq[x_List, a_Integer, b_Integer] := Count[Thread[List[x, RotateLeft[x]]], {a,b}] To see what is going on here: x = {1, 3, 3, 3, 2, 3, 3, 1, 3, 3} RotateLeft[x] is {3, 3, 3, 2, 3, 3, 1, 3, 3, 1} Thread[List[x,RotateLeft[x]]] is {{1,3}, {3,3}, etc] On Wed, Apr 9, 2008 at 5:56 AM, Nasser Abbasi <nma at 12000.org> 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... > > 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 :) > > > -- W. Craig Carter
- References:
- Pattern problem: How to count from a long list of numbers all occurrences of 2 numbers next to each others?
- From: "Nasser Abbasi" <nma@12000.org>
- Pattern problem: How to count from a long list of numbers all occurrences of 2 numbers next to each others?