Re: Appending to Lists

• To: mathgroup at smc.vnet.net
• Subject: [mg27122] Re: [mg27045] Appending to Lists
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Sun, 4 Feb 2001 02:58:57 -0500 (EST)
• References: <qrYe6.930\$l32.23330@ralph.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Nice!
Your code , bin5 below, is nearly twice as quick as mine, bin3, and the
individual bins inherit the original order.
A slight change, bin6, speeds it up further.

bin3[b_,c_]:=Split[Sort[Transpose[{c,b}]],#1[[1]]===#2[[1]]&][[All,All,-1]]

bin3[b,c];//Timing

{18.35 Second,Null}

bin5[b_,c_]:= (Split[Ordering[c], c[[#1]] == c[[#2]]&] /.
n_Integer :> b[[n]])

bin5[b,c];//Timing

{11.75 Second,Null}

bin6[b_,c_]:= b[[#]]&/@Split[Ordering[c], c[[#1]] == c[[#2]]&]

bin6[b,c];//Timing

{7.2 Second,Null}
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

<BobHanlon at aol.com> wrote in message
news:qrYe6.930\$l32.23330 at ralph.vnet.net...
>
> b = {0.2, 0.6, 1.2, -0.2, 0.5, 0.3, 0.7, -0.2, -0.6};
>
> c = {1, 2, 3, 1, 2, 1, 2, 1, 1};
>
> a = (Split[Ordering[c], c[[#1]] == c[[#2]]&] /.
>       n_Integer :> b[[n]])
>
> {{0.2, 0.3, -0.2, -0.2, -0.6}, {0.6, 0.5, 0.7}, {1.2}}
>
> PadRight[#, Max[Length /@ a]]& /@ a
>
> {{0.2, 0.3, -0.2, -0.2, -0.6}, {0.6, 0.5, 0.7, 0, 0},
>   {1.2, 0, 0, 0, 0}}
>
> Bob Hanlon
>
> In a message dated 2001/2/1 3:41:34 AM, j.k.jones at dl.ac.uk writes:
>
> >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).
> >A
> >Position command is applied to list (c) for an element, then with this
> >output the list (a) is created from list (b) at positions given by the
> >element position data, list (c). This is repeated for the large number
> >of
> >elements in the original lists.
> >The Position command is necessary as different elements appear in the
list
> >a
> >different number of times.
> >However, with the large number of elements in the lists (approx 50,000
> >for a
> >simple list), this method is _very_ slow.
> >If any one can give me help in speeding this process up I would be very
> >grateful.
> >
> >The data sets would look like this
> >
> >      b                       c
> >
> >    0.2                      1
> >    0.6                      2
> >    1.2                      3
> >    -0.2                     1
> >    0.5                       2
> >    0.3                       1
> >    0.7                       2
> >   -0.2                      1
> >   -0.6                      1
> >
> >A List would then be created from this data ( the list (a) ) containing
> >vectors for 1, 2 and 3. The data in (b) is not important, and the order
> >in
> >which elements in (c) drop out is not set.
> >In this case the (a) list should look like
> >
> >a = { { 0.2, -0.2, -0.2, -0.6} , {0.6, 0.5, 0.7} , { 1.2 } }
> >
> >My current function looks like this
> >
> >Do[AppendTo[xfinal,
> >      Flatten[Part[X, #] & /@
> >          Position[Global`PARTICLE, i]]], {i, 1,
> >      Max[PARTICLE]}];
> >
> >where xfinal is an (a) list, i.e. to be created.
> >          X is the (b) list , i.e. to be addressed, and
> >          PARTICLE is the (c) list. It is referenced by number.
> >
> >and it is very slow!
> >
> >Also, after producing this list, the different vector elements need to
> >be
> >made the same length, and so 0.0 are added to the ends of all vector
> >elements shorter than the longest. My current function for doing this
looks
> >like
> >
> >table = Table[0.0, {Length[First[long]]}]; Print["Table Created!"];
> >
> >Do[If[Length[Part[xfinal, i]] < Length[First[long]],
> >      AppendTo[Part[xfinal, i],
> >        Drop[table, (Length[Part[xfinal, i]])] ]], {i, 2,
> >      Length[xfinal]}];
> >
> >where list (long) just sorts the list elements according to length.
> >
> >This function is also very slow, and I was wondering, again, if anyone
> >knew
> >a faster way of implementing this. Is the production of a table, once,
> >and
> >then dropping bits off and appending the fastest method? Of course this
> >needs to be done tens of thousands of times per set of data so any small
> >speed increase would be very helpful ;->
> >
>
>

```

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