Re: defining a transform from two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg40878] Re: [mg40837] defining a transform from two lists
- From: Peter <peter1963 at totalise.co.uk>
- Date: Tue, 22 Apr 2003 06:50:50 -0400 (EDT)
- References: <200304211058.GAA22424@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Nathan Moore wrote: >Suppose I have two lists of similar length, > p1={n[i],a[i]} >and > p2 = {n[i],b[i]} >and I'd like to create a new list, > p3 = { n[i], f[ a[i], b[i] ] } >if the new list comes from one parent list then it is relatively simple to >write and apply a transform w/ map like > h[{x_,y_}]:={x,f[y]} > newlist = h/@list or = Map[h,list] >but two parent lists confuse me > >thanks again! > >Nathan Moore, >University of Minnesota Physics > > > > > Hi Nathan, if you want to construct a function like h in your 1-list-example, the trick is to rearrange the lists for simple mapping: In[1]:= p2 = (p1=Table[{n[i],a[i]},{i,5}]) /. a->b; h[{{n_,a_},{n_,b_}}] := {n,f[a,b]}; In[3]:= p3 = h /@ Transpose[{p1,p2}] Out[3]= {{n[1], f[a[1], b[1]]}, {n[2], f[a[2], b[2]]}, {n[3], f[a[3], b[3]]}, {n[4], f[a[4], b[4]]}, {n[5], f[a[5], b[5]]}} You can use a rule; it's a matter of taste: Transpose[{p1,p2}] /. {{n_,x_},{n_,y}} -> {n,f[x,y]} gives the same result. Peter
- References:
- defining a transform from two lists
- From: "Nathan Moore" <nmoore@physics.umn.edu>
- defining a transform from two lists