Re: Table constructed from two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg123732] Re: Table constructed from two lists
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Sat, 17 Dec 2011 02:39:54 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201112161048.FAA06818@smc.vnet.net>
list1 = {1, 2, 3};
list2 = {4, 5, 6};
You need to use Part (e.g., list1[[i]] ) in your Table
Tplus = Table[list1[[i]] + list2[[i]], {i, Range[Length[list1]]}]
{5, 7, 9}
However, since Plus has the attribute Listable, a much simpler way is
list1 + list2
{5, 7, 9}
Times is also Listable
list1*list2
{4, 10, 18}
As is Power
list2^list1
{4, 25, 216}
Bob Hanlon
On Fri, Dec 16, 2011 at 5:48 AM, Alan Forrester <af17g11 at 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
>
- References:
- Table constructed from two lists
- From: Alan Forrester <af17g11@gmail.com>
- Table constructed from two lists