Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7269 > unrolled thread

Table constructed from two lists

Started byAlan Forrester <af17g11@gmail.com>
First post2011-12-16 11:01 +0000
Last post2011-12-17 07:56 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Table constructed from two lists Alan Forrester <af17g11@gmail.com> - 2011-12-16 11:01 +0000
    Re: Table constructed from two lists Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-12-17 07:49 +0000
    Re: Table constructed from two lists "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-17 07:56 +0000

#7269 — Table constructed from two lists

FromAlan Forrester <af17g11@gmail.com>
Date2011-12-16 11:01 +0000
SubjectTable constructed from two lists
Message-ID<jcf8dv$6us$1@smc.vnet.net>
Hello

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 error I get claims that i is not an integer or a list of integers,
but Range[Length[list1]] is a list of integers.

I would appreciate any ideas on what I'm doing wrong.

Alan Forrester

[toc] | [next] | [standalone]


#7305

FromArmand Tamzarian <mike.honeychurch@gmail.com>
Date2011-12-17 07:49 +0000
Message-ID<jchhhj$ihq$1@smc.vnet.net>
In reply to#7269
On Dec 16, 10:01 pm, Alan Forrester <af17...@gmail.com> wrote:
> Hello
>
> 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 error I get claims that i is not an integer or a list of integers,
> but Range[Length[list1]] is a list of integers.
>
> I would appreciate any ideas on what I'm doing wrong.
>
> Alan Forrester

Presumably the absence of Table at the head of your expression is not
present in your actual code? In any case you need to index using Part
which means using double brackets e.g. index[[i]]

But save yourself some time and just add the lists:

list1 + list2

Mike

[toc] | [prev] | [next] | [standalone]


#7318

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2011-12-17 07:56 +0000
Message-ID<jchhuv$imn$1@smc.vnet.net>
In reply to#7269
On Fri, 16 Dec 2011 11:01:19 -0000, Alan Forrester <af17g11@gmail.com>  
wrote:

> Hello
>
> 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 error I get claims that i is not an integer or a list of integers,
> but Range[Length[list1]] is a list of integers.
>
> I would appreciate any ideas on what I'm doing wrong.
>
> Alan Forrester
>

A bit like that, maybe, but not too much, since your example looks more  
like a list comprehension as used in other languages (but not in  
Mathematica).

Simply correcting the syntax, one could write

Tplus = Table[list1[[i]]+list2[[i]], {i, Range@Length[list1]}]

where double square brackets represent part indexing. But iterating over  
the elements is not necessary since many functions in Mathematica are  
automatically aware of tensor inputs (somewhat like APL). So instead one  
can write:

Tplus = list1 + list2

or for that matter

Tcos = list1 Cos[list2]
Tpow = list1^list2

etc.

To find out whether a function can be used in this way, check to see if it  
has the Attribute Listable:

MemberQ[Attributes[Plus], Listable] (* = True *)
MemberQ[Attributes[Times], Listable] (* = True *)
MemberQ[Attributes[Cos], Listable] (* = True *)
MemberQ[Attributes[Power], Listable] (* = True *)

and so forth.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web