       Re: Nested list - adding to each entry

• To: mathgroup at smc.vnet.net
• Subject: [mg113067] Re: Nested list - adding to each entry
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Tue, 12 Oct 2010 04:29:09 -0400 (EDT)

```On 10/11/10 at 5:16 AM, sschmitt at physi.uni-heidelberg.de (Sebastian
Schmitt) wrote:

>I want to add something (an absolute position) to each entry (a
>relative position) of a list. This works as expected in the 1D case:

>In:= 1 + {2, 3, 4}

>Out= {3, 4, 5}

>But the 2D case is different because my list of relative positions
>is interpreted as a matrix:

>In:= {1, 2} + {{a, b}, {c, d}}

>Out= {{1 + a, 1 + b}, {2 + c, 2 + d}}

>I would like to get

>{{1 + a, 2 + b}, {1 + c, 2 + d}}

>I know I can achieve it with the help of a pure function:

>In:= Map[# + {1, 2} &, {{a, b}, {c, d}}]

>Out= {{1 + a, 2 + b}, {1 + c, 2 + d}}

>Is there a builtin function that does the same? I'm somehow
>reluctant to use pure functions too much. Is my concern unfounded?

Here is another way to do the same thing without pure functions

In:= {{a, b}, {c, d}} /. {a_Symbol, b_Symbol} :> {a, b} + {1, 2}

Out= {{a + 1, b + 2}, {c + 1, d + 2}}

But frankly, I would do this as:

In:= # + {1, 2} & /@ {{a, b}, {c, d}}

Out= {{a + 1, b + 2}, {c + 1, d + 2}}

I simply don't see any reason to be concerned about using pure functions.

```

• Prev by Date: Re: local variables - Module, For loop
• Next by Date: Re: If without else
• Previous by thread: Re: Nested list - adding to each entry
• Next by thread: Re: Nested list - adding to each entry