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: [mg113055] Re: Nested list - adding to each entry
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Tue, 12 Oct 2010 04:26:55 -0400 (EDT)
  • References: <i8ukm0$odi$1@smc.vnet.net>

I don't have problems with pure functions, but I can imagine you'd
prefer the slightly clearer and less ambiguous but also less succinct
Function[ ] alternative.

Your problem is caused by Mathematica interpreting the 2D list as a
row of lists just as the vector is a row of numbers. The numbers are
added to the corresponding lists as you would expect. If you want to
have it otherwise you could transpose the matrix before, add the
numbers and transpose again:

({1, 2} + {{a, b}, {c, d}}\[Transpose])\[Transpose]

Cheers -- Sjoerd

On Oct 11, 11:16 am, Sebastian Schmitt <sschm... at physi.uni-
heidelberg.de> 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



  • Prev by Date: Re: If without else
  • Next by Date: Re: GeneralizedLinearModelFit and offsert for poisson
  • Previous by thread: Re: Nested list - adding to each entry
  • Next by thread: Re: Solving a non-linear system to find coefficients in