MathGroup Archive 1997

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

Search the Archive

Re: Threading objects of unequal length

  • To: mathgroup at smc.vnet.net
  • Subject: [mg8136] Re: [mg8112] Threading objects of unequal length
  • From: Wouter Meeussen Vandemoortele CC R&D <wmn.vdmcc at vandemoortele.be>
  • Date: Fri, 15 Aug 1997 23:41:48 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

hi Carl,

it={{a,b}, {c,d,e}, {f}};

how about a one-liner?
Table[Part[it,Flatten[Position[Length/@it,a_/;a>=i]],i],{i,Length[it]}]

{{a, c, f}, {b, d}, {e}}

... or a function:

WollThread[head_,args__List]:=
Table[head@@Part[args,Flatten[Position[Length/@args,a_/;a>=i]],i],
{i,Length[args]}]

test it:

WollThread[h,{{a,b}, {c,d,e}, {f}}]

{h[a, c, f], h[b, d], h[e]}

WollThread[Plus,{{a,b}, {c,d,e}, {f}}]

{a + c + f, b + d, e}

hope it helps some,

wouter.
*************************************************************************
At 12:54  12.08.97 -0400, you wrote:
>Hi group,
>
>I'm interested in adding variable length List. For example, I would like
>to do something like
>
>{a,b}+{c,d,e}+{f}
>
>to return
>
>{a+c+f,b+d,e}
>
>Of course, Plus doesn't work, since you can't thread objects of unequal
>length.
>
>So what is a good way of defining a function to do this? To be specific, I
>would like a function g which accepts multiple arguments, and when given
>an input like
>
>g[{a,b}, {c,d,e}, {f}]
>
>returns
>
>{h[a,c,f],h[b,d],h[e]}
>
>where in my particular application, I want h to be Plus. However, it would
>be nice to have the function work for arbitrary heads.
>
>I could do something like padding each of the arguments with some dummy
>variable so that everything has the same length, then threading, then
>removing the dummy variable, but that seems very cumbersome. Can anyone
>think of a better way?
>
>Thanks for any comments.
>
>Carl Woll
>Physics Dept
>U of Washington
>
>
>
>
>

NV Vandemoortele Coordination Center
Oils & Fats Applied Research
Prins Albertlaan 79
Postbus 40
B-8870 Izegem (Belgium)
Tel: +/32/51/33 21 11
Fax: +/32/51/33 21 75
vdmcc at vandemoortele.be



  • Prev by Date: AW: ListPlot with little plus signs instead?
  • Next by Date: Re: Converting Excel table to Mathematica matrix.
  • Previous by thread: Threading objects of unequal length
  • Next by thread: Re: Threading objects of unequal length