Re: Special Matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg28342] Re: Special Matrix
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Thu, 12 Apr 2001 02:18:06 -0400 (EDT)
- Organization: University of Washington
- References: <9ah5nq$ps0@smc.vnet.net> <9ajmap$s9e@smc.vnet.net> <9b0sso$rq@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Allan,
If further speed improvements are sought, one of the slowest parts of both
functions in your post is the use of Table. A faster way of creating this
table is to again use PadLeft, as in
In[3]:=
t1[n_]:=Table[1,{n},{n}]
t2[n_]:=PadLeft[{{}},{n,n},1]
In[5]:=
Do[t1[100], {1000}]; // Timing
Do[t2[100], {1000}]; // Timing
t1[100] === t2[100]
Out[5]=
{11.719 Second, Null}
Out[6]=
{5.578 Second, Null}
Out[7]=
True
Carl Woll
Physics Dept
U of Washington
"Allan Hayes" <hay at haystack.demon.co.uk> wrote in message
news:9b0sso$rq at smc.vnet.net...
> Souvik,
> Here is another way
>
> PM1[n]:=
> Block[{mat=
Table[1,{n},{n}]},mat[[{1,-1},All]]=0;mat[[All,{1,-1}]]=0;mat]
>
> It seems to be slightly quicker than Carl Woll's (which itself appears to
be
> the fastest of the posting so far)
>
> PM2[n_]:=PadLeft[Table[1,{n-2},{n-2}],{n,n},0,{1,1}]
>
> n=2000;
>
> M1= PM1[n];//Timing
>
> {8.18 Second,Null}
>
> M2= PM2[n];//Timing
>
> {10.71 Second,Null}
>
> Check
>
> M1===M2
>
> 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
>
> "Souvik Banerjee" <s-banerjee at nwu.edu> wrote in message
> news:9ajmap$s9e at smc.vnet.net...
> > Not sure if the shortest but it works:
> >
> > CreateMatrix[n_] :=
> > Block[{r,c,m},
> > Needs["LinearAlgebra`MatrixManipulation`"];
> > r = ZeroMatrix[1, n]; m = Table[1, {n - 2}, {n - 2}];
> > c = ZeroMatrix[n - 2, 1];
> > BlockMatrix[{{r}, {c, m, c}, {r}}]
> > ]
> >
> > The bottleneck seems to be the Table[] operations. If there is a built
in
> > function to create a matrix with a constant element then it will speed
> this
> > up.
> >
> > -Souvik
> >
> >
> > Yoram Pollack <syftech at saad.org.il> wrote in message
> > news:9ah5nq$ps0 at smc.vnet.net...
> > > Hello
> > > I am trying to create a "n x n" matrix with "1" in every position
exept
> in
> > > first and last row, and first and last coulumn, where "0" is needed.
In
> > > other words: sqare matrix full of "1" sorounded by "0".
> > > What will be the fastest and shortest way to do it?
> > >
> > > Thanks
> > > Yoram
> > >
> > >
> > >
> >
> >
> >
>
>
>