Re: Re: Threading objects of unequal length
- To: mathgroup at smc.vnet.net
- Subject: [mg8243] Re: [mg8136] Re: [mg8112] Threading objects of unequal length
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Thu, 21 Aug 1997 21:16:59 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Further to my previous posting, here's another, faster, way - ten
times as fast:
(I use the name MapThreadRagged because function is more like
MapThread than Thread)
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}]
]
Correctness:
MapThreadRagged[h,{{a,b}, {c,d,e}, {f},{g}}]
{h[a,c,f,g],h[b,d],h[e]}
MapThreadRagged[h,{{a,b}, {c,d,e}}]
{h[a,c],h[b,d],h[e]}
Timings - comparison with Wouter's WollThread2 from previous posting
WollThread2[head_,args__List]:=
Table[head@@Part[args,Flatten[Position[Length/@args,a_/;a>=i]],i],
{i, Max[Length/@args]}]
tst = Table[Range[Random[Integer,100]],{100}];
(mtr = MapThreadRagged[h,tst]);//Timing
{0.5156 Second,Null}
(wt = WollThread2[h,tst]);//Timing
{5.26479 Second,Null}
Check
mtr == wt
True
Allan
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