MathGroup Archive 2007

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

Search the Archive

Re: Map function which adds last two numbers of a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73553] Re: [mg73530] Map function which adds last two numbers of a list
  • From: Adriano Pascoletti <pascolet at dimi.uniud.it>
  • Date: Wed, 21 Feb 2007 06:05:30 -0500 (EST)
  • References: <200702210646.BAA18238@smc.vnet.net>

Christopher,

here is a solution based on Replace

In[1]:=
z = {{1, 4, 5, 6}, {7, 8, 9, 1}, {1, 2, 4, 3}};
   Replace[z, {pre___, l2_, l1_} :> {pre, l1 + l2}, {1}]

Out[1]=
{{1, 4, 11}, {7, 8, 10}, {1, 2, 7}}

and another one based on mapping ReplaceAll

In[2]:=
z = {{1, 4, 5, 6}, {7, 8, 9, 1}, {1, 2, 4, 3}};
   (#1 /. {pre___, l2_, l1_} :> {pre, l1 + l2} & ) /@ z

Out[2]=
{{1, 4, 11}, {7, 8, 10}, {1, 2, 7}}

Adriano Pascoletti

On 21 feb 2007, at 07:46, Christopher Pike wrote:

>
> Hi,
>   Consider this:
>
> z = {{1,4,5,6},{7,8,9,1},{1,2,4,3}}
>
> I'd like to Map a function onto z which would replace the last two  
> items
> with their sum:
>
> {{1,4,11},{7,8,10},{1,2,7}}
>
> I could easily use the Table command to construct this new table,  
> but it
> would be nicer if I new how to Map some function on z that would  
> produce
> the same result.
>
> Any suggestions.
>
> Thanks, Chris Pike
>



  • Prev by Date: Re: Quick integral.
  • Next by Date: Re: Showing that ArcSinh[2]/ArcCsch[2] is 3?
  • Previous by thread: Map function which adds last two numbers of a list
  • Next by thread: Re: Map function which adds last two numbers of a list