Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

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[43]:= 1 + {2, 3, 4}

>Out[43]= {3, 4, 5}

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

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

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

>I would like to get

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

>instead.

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

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

>Out[58]= {{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?

What is your concern about usage of pure functions?

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

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

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

But frankly, I would do this as:

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

Out[7]= {{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