MathGroup Archive 2002

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

Search the Archive

RE: RE: Joining lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37851] RE: [mg37824] RE: [mg37810] Joining lists
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Fri, 15 Nov 2002 01:35:06 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Pigeon, Robert [mailto:Robert.Pigeon at drdc-rddc.gc.ca]
To: mathgroup at smc.vnet.net
>Sent: Thursday, November 14, 2002 12:11 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg37851] [mg37824] RE: [mg37810] Joining lists
>
>
>Sorry...  I just cut and paste from a Notebook!
>	
>	Here what I am trying to do:
>		a={1,2,3};
>		b={4,5,6};
>		c={{a[[1]],b[[1]]},{a[[2]],b[[2]]},{a[[3]],b[[3]]}}
>			{{1,4},{2,5},{3,6}}
>
>I tried:   Do[{{a[[i]],b[[i]]}},{i,3}]    And it is not working.
>
>Since my first posting I found that Transpose[{a,b}]  will do it.
>
>But I would like to know why the Do loop is not doing it !
>
>Thanks,
>
>Robert
>

Robert,

the Do[...] does not deliver a value! You have to make an assignment, e.g.

In[1]:= a = {1, 2, 3};
        b = {4, 5, 6};
        c = {, ,};

In[4]:= Do[c[[i]] = {a[[i]], b[[i]]}, {i, 3}]

In[5]:= c
Out[5]= {{1, 4}, {2, 5}, {3, 6}}

Or (just for purposes of demonstration):

In[6]:= c = {}

In[7]:= Do[AppendTo[c, {a[[i]], b[[i]]}], {i, 3}]

In[8]:= c
Out[8]= {{1, 4}, {2, 5}, {3, 6}}

In[4] might be quite performant (more so if compiled, but then initialize c
numerically), whereas In[7] certainly is an Anti-Pattern which is to be
avoided. Best of course is to use Transpose, as you did.


--
Hartmut






>-----Original Message-----
>From: Pigeon, Robert [mailto:Robert.Pigeon at drdc-rddc.gc.ca]
To: mathgroup at smc.vnet.net
>To: mathgroup at smc.vnet.net
>Subject: [mg37851] [mg37824] [mg37810] Joining lists
>
>
>Good day,
>	Here what I am trying to do:
>		 <<...OLE_Obj...>> 
>		 <<...OLE_Obj...>> 
>		 <<...OLE_Obj...>> 
>			 <<...OLE_Obj...>> 
>Of course my two lists are a lot larger !  So, I would like to 
>build the new
>list c automatically, from the first item to the last of my 
>lists (number of
>items is equal in both lists).  I tried:
>		 <<...OLE_Obj...>> 
>		 <<...OLE_Obj...>> 
> and other things.  But it does not work.
>
>At the end I want to plot (ListPlot) the new list.
>
>Any suggestion?
>
>Thanks
>
>
>Robert Pigeon
>
>



  • Prev by Date: RE: Characters Allowed in Symbols
  • Next by Date: Re: Joining lists
  • Previous by thread: Re: RE: Joining lists
  • Next by thread: Re: Joining lists