MatrixTemplate (was SpecialMatrix)
- To: mathgroup at smc.vnet.net
- Subject: [mg28414] MatrixTemplate (was SpecialMatrix)
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Mon, 16 Apr 2001 23:54:17 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
The problem of creating a matrix with all the entries the same, say all
zero, has cropped up in recent postings.
The following seems to be very quick way of doing this:
Clear["`*"]
CreateMatrix[x_,m_,n_]:=
PadRight[{#},m,{#}]&[PadRight[{x},{n},{x}]]
Check
CreateMatrix[0,3,5]
{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}
Timings
CreateMatrix[0,5000,5000];//Timing
CreateMatrix[0,5000,2500];//Timing
CreateMatrix[0,2500,5000];//Timing
CreateMatrix[0,5000,250];//Timing
CreateMatrix[0,250,5000];//Timing
{0. Second,Null}
{0.11 Second,Null}
{0. Second,Null}
{0.06 Second,Null}
{0.16 Second,Null}
For comparison:
IdentityMatrix[5000];//Timing
{10.27 Second,Null}
and. using Table,
Clear["`*"]
CreateMatrix[x_,m_,n_]:=
Table[Evaluate[Table[x,{n}]],{m}]
CreateMatrix[0,3,5]
{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}
CreateMatrix[0,5000,5000];//Timing
CreateMatrix[0,5000,2500];//Timing
CreateMatrix[0,2500,5000];//Timing
CreateMatrix[0,5000,250];//Timing
CreateMatrix[0,250,5000];//Timing
{10.22 Second,Null}
{5. Second,Null}
{4.67 Second,Null}
{0.44 Second,Null}
{0.55 Second,Null}
--
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