MathGroup Archive 2001

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

Search the Archive

MatrixTemplate (was SpecialMatrix)


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





  • Prev by Date: Re: matrix
  • Next by Date: Can Mathematica be used as expert system (rule-based computations like CLIPS?)
  • Previous by thread: Re: conditionals during nonlinear regression
  • Next by thread: RE: MatrixTemplate (was SpecialMatrix)