Re: Special Matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg28328] Re: Special Matrix
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 11 Apr 2001 02:01:07 -0400 (EDT)
- References: <9ah5nq$ps0@smc.vnet.net> <9ajmap$s9e@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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 > > > > > > > > >