Re: Map function which adds last two numbers of a list
- To: mathgroup at smc.vnet.net
- Subject: [mg73578] Re: Map function which adds last two numbers of a list
- From: "dimitris" <dimmechan at yahoo.com>
- Date: Thu, 22 Feb 2007 04:34:09 -0500 (EST)
- References: <ergqrb$jg0$1@smc.vnet.net>
Hi. Along many other alternatives you can try In[119] := z /. {x_, y_, z_, w_} :> {x, y, z + w} Out[119] = {{1, 4, 11}, {7, 8, 10}, {1, 2, 7}} In[120] := ({#1[[1]], #1[[2]], #1[[3]] + #1[[4]]} &) /@ z Out[120] = {{1, 4, 11}, {7, 8, 10}, {1, 2, 7}} In[216]:= {z[[#, 1]], z[[#, 2]], (Plus @@@ Reap[{Sow[#[[3]]], Sow[#[[4]]]} & /@ z][[1]])[[#]]} & /@ Range[3] Out[216]= {{1, 4, 11}, {7, 8, 10}, {1, 2, 7}} However pay special attention to what Andrzej Kozlowski mentions you in his post! Dimitris =CF/=C7 Christopher Pike =DD=E3=F1=E1=F8=E5: > 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