Re: Nested list - adding to each entry
- To: mathgroup at smc.vnet.net
- Subject: [mg113206] Re: Nested list - adding to each entry
- From: Sebastian Schmitt <sschmitt at physi.uni-heidelberg.de>
- Date: Tue, 19 Oct 2010 05:53:19 -0400 (EDT)
- References: <201010110916.FAA25013@smc.vnet.net>
Dear all!
Thanks for all the answers. Is there a reason nobody came up with a
solution using Table?
abspos = {1,2}
relpos = {{a, b}, {c, d}}
Table[abspos + rel, {rel,relpos}]
This looks to me to be the "best" way. In terms of readability/simplicity.
Cheers,
Sebastian
PS: I learned this "iterator"-like feature of Table after I've asked.
Sebastian Schmitt wrote:
> Dear all!
>
> (I recycle my disclaimer.)
>
> I'm new to Mathematica with a background mostly in C++. Many times I
> have the impression that my style is not natural-Mathematica
> (Mathematicaesque so to say).
>
> 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?
>
> Thanks in advance,
>
> Sebastian
- References:
- Nested list - adding to each entry
- From: Sebastian Schmitt <sschmitt@physi.uni-heidelberg.de>
- Nested list - adding to each entry