Re: Joining lists
- To: mathgroup at smc.vnet.net
- Subject: [mg37860] Re: Joining lists
- From: adam.smith at hillsdale.edu (Adam Smith)
- Date: Fri, 15 Nov 2002 01:36:15 -0500 (EST)
- References: <aqss4p$c4t$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Robert, You seem to have the same difficulties that I had adapting to Mathematica. I blame mine on learning to program in FORTRAN back in the dark ages. If I am totally off-base with my explanation, please ignore this and forgive me. But here is how I understand what is happening. a={1,2,3}; b={4,5,6}; c={{a[[1]],b[[1]]},{a[[2]],b[[2]]},{a[[3]],b[[3]]}} This works because you are explicitly defining c. With the equal sign and a completed list Mathematica can create the 3X2 list c and assign the values. In effect Mathematica treats "=" very intelligently and does a great deal behind the scenes. However the Do construction is "dumber" in a sense. There is no assignment to a variable name inside the do loop. Mathematica indeed does the steps, but does not store them anywhere. You can see this by Print[ ] Do[Print[ {a[[i]], b[[i]]}], {i, 3}] The correct element print out, but then "evaporate". With a Do construction, you must first create an appropriate sized variable name. Then you make the assignment. I use the name "d" in the example below. d = Array[ , {3, 2}] (* Creates a null 3X2 array named d *) Do[d[[i, 1]] = a[[i]]; d[[i, 2]] = b[[i]];, {i, 3}] This will do what you want. Note that I made two assignments in the do loop. But as others will point out, it is not the way to do it in Mathematica. You discovered the Transpose way. One can also use: f = Thread[{a,b}] Thread and Transpose have the big advantage that you don't have to know the size of null array before preceeding. They are also much quicker than the Do loop. With Transpose being the quickest, I believe. Adam Smith "Pigeon, Robert" <Robert.Pigeon at drdc-rddc.gc.ca> wrote in message news:<aqss4p$c4t$1 at smc.vnet.net>... > 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