MathGroup Archive 1999

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

Search the Archive

Re: Finding lengths of identical sequences in lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20265] Re: [mg20240] Finding lengths of identical sequences in lists
  • From: BobHanlon at aol.com
  • Date: Sun, 10 Oct 1999 01:27:25 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Joel,

See on-line help for "run length encoding" (Section 1.8.10).

x = {3, 2, 2, 3, 3, 3, 1};

Length /@ Split[x]

{1, 2, 3, 1}

rle[x_List?VectorQ] := {First[#], Length[#]} & /@ Split[x]

expandRLE[x_List /; ! VectorQ[x]] := Flatten[Table[#[[1]], {#[[2]]}] & /@ x]

rle[x]

{{3, 1}, {2, 2}, {3, 3}, {1, 1}}

expandRLE[%] == x

True

x = Table[Random[Integer, {1, 3}], {1000}];

expandRLE[rle[x]] == x

True


Bob Hanlon

In a message dated 10/8/1999 10:23:42 PM, jcannon at jcannon.washjeff.edu writes:

>I am looking for a more elegant solution to the following. I have a
>list (a list of positions at consecutive times).  I wish to create a
>new list of the length of each sequence of consecutive identical
>members (including those of length 1). 
>
>For example, for the list {3,2,2,3,3,3,1} I would create the list
>{1,2,3,1} because the first 3 appears once, followed by two 2's,
>etc. The lists I was working with contained a list of numbers
>(e.g. {{x1,y1},{x2,y2},...} rather than simple numbers. I accomplished
>this using a while loop, but I suspect there are better ways to do
>this using pattern matching or other approaches.
>


  • Prev by Date: How to get 4.0 to own notebooks?
  • Next by Date: Re: Replacing Part of a Matrix
  • Previous by thread: Re: Finding lengths of identical sequences in lists
  • Next by thread: Re: Finding lengths of identical sequences in lists