Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding columns and rows to a table

2 views
Skip to first unread message

Philip M. Howe

unread,
Mar 21, 2002, 9:49:21 AM3/21/02
to
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 pmh...@lanl.gov
Mail Stop P945

Jens-Peer Kuska

unread,
Mar 22, 2002, 4:12:59 AM3/22/02
to
Hi,

is

mm = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

PadRight[#, 6, Last[#]] & /@ mm

elegant ??

Regards
Jens

Carl K. Woll

unread,
Mar 22, 2002, 4:23:48 AM3/22/02
to
Hi Philip,

The function PadRight may prove useful for you.

For example, with

tab1 = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

we can add two rows with

PadRight[tab1, Dimensions[tab1]+{2,0},List[tab1[[-1]]]]

and we can add two columns with

PadRight[tab1, Dimensions[tab1]+{0,2},List/@tab1[[All,-1]]]

The List statement in the first example is actually unnecessary, but makes
it clear how to go about adding two columns as in the second example.

Carl Woll
Physics Dept
U of Washington

"Philip M. Howe" <pmh...@lanl.gov> wrote in message
news:a7crth$hug$1...@smc.vnet.net...

Allan Hayes

unread,
Mar 22, 2002, 4:31:36 AM3/22/02
to
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
h...@haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Philip M. Howe" <pmh...@lanl.gov> wrote in message
news:a7crth$hug$1...@smc.vnet.net...

Paul

unread,
Mar 22, 2002, 4:33:45 AM3/22/02
to
Philip,

Try this:

f[a_?MatrixQ] :=
Join[#, #[[{-1, -1}]]] & /@ Join[a, a[[{-1, -1}]]]

tbl = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

f[tbl] // MatrixForm


Paul

Brian Higgins

unread,
Mar 22, 2002, 4:44:31 AM3/22/02
to
Phil, try this using rules

mylist = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

In[2]:=mylist /. {x__Integer, y_Integer} -> {x, y, y, y} /. {x__, y_}
-> {x, y, y, y}

Out[2]={{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}}

Cheers,
Brian


"Philip M. Howe" <pmh...@lanl.gov> wrote in message news:<a7crth$hug$1...@smc.vnet.net>...

BobH...@aol.com

unread,
Mar 22, 2002, 4:48:40 AM3/22/02
to

In a message dated 3/21/02 12:52:37 PM, pmh...@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

Harvey P. Dale

unread,
Mar 22, 2002, 4:51:44 AM3/22/02
to
Philip:
This may help:
cols[l_List]:=Module[{tab2=Flatten/@({#,{#[[-1]]},
{#[[-1]]}}&/@l)},Join[tab2,{Last[tab2]},{Last[tab2]}]]
Best,
Harvey
Harvey P. Dale
University Professor of Philanthropy and the Law
Director, National Center on Philanthropy and the Law
New York University School of Law
Room 206A
110 West 3rd Street
New York, N.Y. 10012-1074

-----Original Message-----
From: Philip M. Howe [mailto:pmh...@lanl.gov]
Subject: Adding columns and rows to a table

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

________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

Wolf, Hartmut

unread,
Mar 24, 2002, 1:47:46 AM3/24/02
to

Phil,

look at

In[12]:= napp = 2;
In[13]:=
Nest[Transpose[Join[#, Table[Last[#], {napp}]]] &, tab1, 2] // MatrixForm

You may tune napp, but not the second "2" appearing.

--
Hartmut Wolf


Allan Hayes

unread,
Mar 24, 2002, 2:08:36 AM3/24/02
to
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
h...@haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


<BobH...@aol.com> wrote in message news:a7eulo$mv3$1...@smc.vnet.net...

0 new messages