MathGroup Archive 2011

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

Search the Archive

Re: Table constructed from two lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123757] Re: Table constructed from two lists
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Sat, 17 Dec 2011 02:48:36 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

On 12/16/11 at 5:48 AM, af17g11 at gmail.com (Alan Forrester) wrote:

>I'd like to construct a table by making each element of the table a
>function of elements of a couple of lists I have specified, but I'm
>getting an error message that seems not to make any sense.

>So I have a couple of lists, like these
>list1 = {1,2,3}
>list2 = {4,5,6}

>My code for the table I want to construct from those lists is a bit
>like this:
>Tplus = [list1[i]+list2[i], {i, Range[Length[list1]]}].

The above simply isn't correct syntax for what you want to do.

You cannot interchange square brackets ([]) with braces ({}).
Square brackets are never used in Mathematica to group things.
Using a single square bracket i.e., x[k] is interpreted by
Mathematica as the *function* named x evaluated at k. If you
want to select the nth element of a list, you need to use double
square brackets. That is x[[n]] selects the nth part of x.

Braces are used to define lists. The synatax

{i, Range[Length[list1]]} evaluates to

{i, {1,2,3,4}}

and does not assign these values to i. If you want to add the
two lists element by element, you could do it this way:

In[7]:= list1 = {1, 2, 3};
list2 = {4, 5, 6};

In[9]:= Table[list1[[n]] + list2[[n]], {n, Length@list1}]

Out[9]= {5,7,9}

But it is far simpler and more efficient to do:

In[10]:= list1 + list2

Out[10]= {5,7,9}

Most functions in Mathematica have the property Listable and
will operate on each element of a list without need to
explicitly select each element. For example you can do:

In[11]:= list1^2 + list2^2

Out[11]= {17,29,45}

=46ar simpler and more readable than explicitly selecting each
element of the lists.




  • Prev by Date: Re: Printing Mathematica Notebooks and WYSIWYG
  • Next by Date: Re: Table constructed from two lists
  • Previous by thread: Re: Table constructed from two lists
  • Next by thread: Re: Table constructed from two lists