MathGroup Archive 2002

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

Search the Archive

RE: Re: Joining lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37895] RE: [mg37860] Re: Joining lists
  • From: "Pigeon, Robert" <Robert.Pigeon at drdc-rddc.gc.ca>
  • Date: Sun, 17 Nov 2002 06:44:45 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

To all of those that replied,  THANKS!  Your answers made me realized that I
have to be careful in my assumptions with Mathematica.  As Adam mentioned, I
was raised with FORTRAN IV ... so I have to be careful on what I'm doing
with expressions that I use in other languages.
Thanks again,

Robert

-----Original Message-----
From: adam.smith at hillsdale.edu [mailto:adam.smith at hillsdale.edu]
To: mathgroup at smc.vnet.net
Subject: [mg37895] [mg37860] Re: Joining lists


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



  • Prev by Date: Re: Random number Newbie Question
  • Next by Date: Re: Problem with NDSolve
  • Previous by thread: Re: Joining lists
  • Next by thread: Scaled PostScript output without margins