MathGroup Archive 1997

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

Search the Archive

Re: A generalized Transpose?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg9204] Re: A generalized Transpose?
  • From: Robert Villegas <villegas>
  • Date: Tue, 21 Oct 1997 02:03:17 -0400
  • Organization: Wolfram Research
  • Sender: owner-wri-mathgroup at wolfram.com

> What I have is a list of structures of identical form, but each structure
> is not rectangular. An example with only two structures is
> ys={{a1,{b1,{c1,d1}}},{a2,{b2,{c2,d2}}}};
> 
> My question is, How can I make the similar change to this list, to obtain:
> ysT={{a1,a2},{{b1,b2},{{c1,c2},{d1,d2}}}}; 


One of the best ways to apply a function to, or group, corresponding
elements of lists is to use a Listable function.  This will work when
the "identical form" is an irregularly nested list (the heads are
List), which is true for your example:

In[6]:= ys={{a1,{b1,{c1,d1}}},{a2,{b2,{c2,d2}}}};

In[7]:= Attributes[f] = Listable;

In[8]:= f @@ ys

Out[8]= {f[a1,a2],{f[b1,b2],{f[c1,c2],f[d1,d2]}}}

In[9]:= % /. f -> List

Out[9]= {{a1,a2},{{b1,b2},{{c1,c2},{d1,d2}}}}


f here is just a dummy function used to glue the structures together.
Since its purpose is temporary, this procedure can be shrink-wrapped
into a small function:

Glue[h_, {lists__List}] :=
Module[{f},
  Attributes[f] = Listable;
  f[lists] /. f -> h
]


Here's how to use Glue on your lists:

In[11]:= Glue[List, ys]

Out[11]= {{a1,a2},{{b1,b2},{{c1,c2},{d1,d2}}}}


Robby Villegas


  • Prev by Date: Re: Mathematica Graphics
  • Next by Date: Re: Divergence and Dirac Delta Function
  • Previous by thread: A generalized Transpose?
  • Next by thread: Re: A generalized Transpose?