Re: Adding columns and rows to a table
- To: mathgroup at smc.vnet.net
- Subject: [mg33516] Re: Adding columns and rows to a table
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sun, 24 Mar 2002 01:44:48 -0500 (EST)
- References: <a7eulo$mv3$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Bob, A variant of your replacement method: Replace[data, {x___, y_} -> {x, y, y, y}, {0, 1}] -- 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 <BobHanlon at aol.com> wrote in message news:a7eulo$mv3$1 at smc.vnet.net... > > In a message dated 3/21/02 12:52:37 PM, pmhowe at lanl.gov writes: > > >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. > > > > tab1=Partition[Range[12],4]; > > tab1 /. > > {x__, y_?AtomQ}->{x,y,y,y} /. > > {x__,y_}->{x,y,y,y} > > Which can be generalized readily to add n columns and m rows > > n=3; m=4; > > tab1 /. {x__, y_?AtomQ}-> > {x,Sequence@@Table[y,{n+1}]} /. > > {x__,y_}->{x,Sequence@@Table[y,{m+1}]} > > Or > > Join[ > tab1c=Join[#,{Last[#]},{Last[#]}]& /@ tab1, > {Last[tab1c]},{Last[tab1c]}] > > Which can be generalized readily to add n columns and m rows > > Join[ > tab1c=Join[#,Sequence@@ > Table[{Last[#]},{n}]]& /@ tab1, > Sequence@@Table[{Last[tab1c]},{m}]] > > > Bob Hanlon > Chantilly, VA USA >