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
is
mm = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
PadRight[#, 6, Last[#]] & /@ mm
elegant ??
Regards
Jens
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...
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...
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
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>...
>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
-----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
(505) 665-5332
(505) 667-9498
Fax: 505-665-5249
email pmh...@lanl.gov
Mail Stop P945
________________________________________________________________________
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
________________________________________________________________________
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
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...