Re: Adding columns and rows to a table
- To: mathgroup at smc.vnet.net
- Subject: [mg33476] Re: Adding columns and rows to a table
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Fri, 22 Mar 2002 04:07:15 -0500 (EST)
- References: <a7crth$hug$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Philip, Here I time a different way of doing what you want: data= Table[Random[Integer,{0,9}],{500},{600}]; (a1= data[[Join[#1,{-1,-1}]]][[All,Join[#2,{-1,-1}]]]&@@(Range/@ Dimensions[data]) );//Timing {0.27 Second,Null} Compare with your code tab1=data; (a2= (tab1c = Transpose[ Append[Append[Transpose[tab1], Transpose[tab1][[-1]]], Transpose[tab1][[-1]]]]; tab1d = Append[Append[tab1c, tab1c[[-1]]], tab1c[[-1]]] ) );//Timing {3.13 Second,Null} Check for correctness: a1===a2 True -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Philip M. Howe" <pmhowe at lanl.gov> wrote in message news:a7crth$hug$1 at smc.vnet.net... > Hello, > > I want to add to a table two columns that match the last column, and > two rows that match the last row. > > Thus, if my table is > > {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; I want to modify it such that I > end up with > > {{1,2,3,4,4,4}, {5,6,7,8,8,8}, {9,10,11,12,12,12}, { > 9,10,11,12,12,12}, {9,10,11,12,12,12}}; > > This works: > > tab1 = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; > > tab1c = Transpose[ > Append[Append[Transpose[tab1], Transpose[tab1][[-1]]], > Transpose[tab1][[-1]]]]; > tab1d = Append[Append[tab1c, tab1c[[-1]]], tab1c[[-1]]] // TableForm > > > However, I bet there are much more efficient and elegant ways of > doing this. Any suggestions? > > Thanks in advance for the help. The ideas I obtain from you folks > are extremely helpful. > > Regards, > > Phil > -- > Philip M. Howe > Program Manager, Stockpile Surety > Los Alamos National Laboratory > > (505) 665-5332 > (505) 667-9498 > Fax: 505-665-5249 > email pmhowe at lanl.gov > Mail Stop P945 >