MathGroup Archive 2009

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

Search the Archive

Re: Modifying List Elements.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98966] Re: Modifying List Elements.
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Wed, 22 Apr 2009 06:33:15 -0400 (EDT)
  • References: <gsmn1l$d3l$1@smc.vnet.net>

nick.maj at mssm.edu wrote:
> how can i add a value to an element in lists?
> 
> data:
> {{0,1},{1,2}}
> 
> needed output:
> {{0,1.5},{1,2.5}}
> 
> thanks in advance, nick.
> 

In place modification:

In[1]:= list = {{0, 1}, {1, 2}}
Out[1]= {{0, 1}, {1, 2}}

In[2]:= list[[All, 2]] += 0.5
Out[2]= {1.5, 2.5}

In[3]:= list
Out[3]= {{0, 1.5}, {1, 2.5}}

Some functional ways:

In[1]:= list = {{0, 1}, {1, 2}}
Out[1]= {{0, 1}, {1, 2}}

In[2]:= {#1, #2 + 0.5} & @@@ list
Out[2]= {{0, 1.5}, {1, 2.5}}

In[3]:= # + {0, 0.5} & /@ list
Out[3]= {{0, 1.5}, {1, 2.5}}

In[4]:= Transpose[Transpose[list] + {0, 0.5}]
Out[4]= {{0, 1.5}, {1, 2.5}}


  • Prev by Date: Re: How to get values from Manipulate
  • Next by Date: Re: How to get values from Manipulate
  • Previous by thread: Re: Modifying List Elements.
  • Next by thread: Re: Modifying List Elements.