MathGroup Archive 2001

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

Search the Archive

Re: Special Matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28345] Re: Special Matrix
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Thu, 12 Apr 2001 02:18:08 -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,

I was thinking about your method a bit more, and came up with the
alternative PM3.

In[1]:=
PM1[n_]:=Block[{mat=Table[1,{n},{n}]},
    mat[[{1,-1},All]]=0;
    mat[[All,{1,-1}]]=0;
    mat]

PM2[n_]:=PadLeft[Table[1,{n-2},{n-2}],{n,n},0,{1,1}]

PM3[n_]:=Block[{mat=IdentityMatrix[n]},
    mat[[All,All]]=1;
    mat[[{1,-1},All]]=0;
    mat[[All,{1,-1}]]=0;
    mat]

Basically, as I said before, the Table construct is quite slow. However,
instead of using PadLeft to construct the table of 1's as I described in my
last post, why not use your method to construct the table of 1's? So, that's
the change in PM3. The result is quite a bit faster, witness:

In[4]:=
Do[r1=PM1[1000],{10}]//Timing
Do[r2=PM2[1000],{10}]//Timing
Do[r3=PM3[1000],{10}]//Timing
r1===r2===r3

Out[4]=
{11.688 Second, Null}

Out[5]=
{13.421 Second, Null}

Out[6]=
{2.469 Second, Null}

Out[7]=
True

So, the fastest way (so far) to create a matrix of 1's is to create a dummy
matrix (IdentityMatrix[n]) and then to change all the entries to 1.

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
> > >
> > >
> > >
> >
> >
> >
>
>
>




  • Prev by Date: Re: Statistics on Matrices
  • Next by Date: Re: how applicate a function to a list??
  • Previous by thread: Re: Special Matrix
  • Next by thread: Optica