MathGroup Archive 2001

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

Search the Archive

Re: Appending to Lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27059] Re: Appending to Lists
  • From: Tom Burton <tburton at cts.com>
  • Date: Sat, 3 Feb 2001 04:58:54 -0500 (EST)
  • Organization: Brahea Consulting
  • References: <95b686$4mr@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hello,

Here is a straighforward improvement that seems adequately fast for the modest lists you are using so far. As an example, consider the following lists b and c:

In[66]:= b = Array[B, {10}]
Out[66]= {B[1], B[2], B[3], B[4], B[5], 
          B[6], B[7], B[8], B[9], B[10]}

In[67]:= c = Table[Random[Integer,{1,3}],{10}]
Out[67]= {1, 2, 1, 2, 2, 1, 3, 3, 2, 2}

I assume that lists b and c are necessarily of equal length, so we can combine them to speed processing. Step 1 is to Sort and then Split the combined list:
In[68]:= temp1 = Split[Sort[Transpose[{c, b}]], First[#1] == First[#2] & ]
Out[68]=
 {{{1, B[1]}, {1, B[3]}, {1, B[6]}}, 
  {{2, B[2]}, {2, B[4]}, {2, B[5]}, {2, B[9]}, {2, B[10]}}, 
  {{3, B[7]}, {3, B[8]}}}

Step 2 is to strip elements of c from the result:
In[69]:=temp2 = Map[Last, temp1, {2}]
Out[69]=
 {{B[1], B[3], B[6]}, 
  {B[2], B[4], B[5], B[9], B[10]}, 
  {B[7], B[8]}}

Step 3 is to append zeroes as needed

In[71]:= 
a = With[{L = Max[Length /@ temp2]}, 
      Map[Join[#1, Table[0.0, {L - Length[#1]}]] &, temp2]
    ]
Out[71]=
 {{B[1], B[3], B[6], 0., 0.}, 
  {B[2], B[4], B[5], B[9], B[10]},
  {B[7], B[8], 0., 0., 0.}}

Step 1 requires about 2.4 seconds to process lists of length 50000 on my 400 MHz PC. The times for steps 2 and 3 are negligible.

On 1 Feb 2001 03:20:22 -0500, in comp.soft-sys.math.mathematica you wrote:

>Hi,
>
>I have a function that creates a list (a) from another list (b). The list
>elements are re-grouped in the new list according to a third list (c)...
Tom


  • Prev by Date: Getting parameters from Fit[]..
  • Next by Date: Re: Graphic problem
  • Previous by thread: Re: Appending to Lists
  • Next by thread: Re: Appending to Lists