MathGroup Archive 2002

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

Search the Archive

Re: Adding columns and rows to a table

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33491] Re: [mg33439] Adding columns and rows to a table
  • From: BobHanlon at aol.com
  • Date: Fri, 22 Mar 2002 04:08:56 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

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


  • Prev by Date: Re: Interpolation problem
  • Next by Date: Re: Adding columns and rows to a table
  • Previous by thread: Re: Adding columns and rows to a table
  • Next by thread: Re: Adding columns and rows to a table