Re: Re: Threading objects of unequal length
- To: mathgroup at smc.vnet.net
- Subject: [mg8253] Re: [mg8163] Re: [mg8112] Threading objects of unequal length
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Sat, 23 Aug 1997 01:58:50 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Here is a way of tidying up at the end of my previous code that is
more in keeping with Olivier's suggestion for using neutral elements
- its also a little faster:
MapThreadRagged2[h_,d_] :=
Module[{x = Sequence[],ml,pd,tb},
Block[{x},
ml = Max[Length /@ d];
pd = Table[x,{ml}];
tb = Take[Join[#,pd],ml]&/@d;
MapThread[h, tb]
]
]
instead of
MapThreadRagged[h_,d_] :=
Module[{x,ml,pd,tb,mth},
ml = Max[Length /@ d];
pd = Table[x,{ml}];
tb = Take[Join[#,pd],ml]&/@d;
mth = MapThread[h, tb];
DeleteCases[mth, x, {2}]
]
Timings
tst= Table[Range[Random[Integer,100]],{100}];
(mtr2 =MapThreadRagged2[h,tst]);//Timing
{0.437238 Second,Null}
(mtr =MapThreadRagged[h,tst]);//Timing
{0.562517 Second,Null}
Check
mtr == mtr2
True
Allan Hayes
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk/training.html
voice:+44 (0)116 2714198
fax: +44 (0)116 2718642
Leicester, UK