Mathematica 9 is now available
Previous section-----Next section

3.7.1 Constructing Matrices

Table[f, {i, m}, {j, n}] build an  matrix where f is a function of i and j that gives the value of the   entry
Array[f, {m, n}] build an  matrix whose   entry is f[i, j]
DiagonalMatrix[list] generate a diagonal matrix with the elements of list on the diagonal
IdentityMatrix[n] generate an  identity matrix
Normal[SparseArray[{{ ,  }-> , { ,  }-> , ... }, {m, n}]]
make a matrix with non-zero values  at positions { ,  }

Functions for constructing matrices.
This generates a  matrix whose   entry is a[i, j].

In[1]:=  Table[a[i, j], {i, 2}, {j, 2}]

Out[1]=

Here is another way to produce the same matrix.

In[2]:=  Array[a, {2, 2}]

Out[2]=

DiagonalMatrix makes a matrix with zeros everywhere except on the leading diagonal.

In[3]:=  DiagonalMatrix[{a, b, c}]

Out[3]=

IdentityMatrix[n] produces an  identity matrix.

In[4]:=  IdentityMatrix[3]

Out[4]=

This makes a  matrix with two non-zero values filled in.

In[5]:=  Normal[SparseArray[{{2, 3}->a, {3, 2}->b}, {3, 4}]]

Out[5]=

MatrixForm prints the matrix in a two-dimensional form.

In[6]:=  MatrixForm[%]

Out[6]//MatrixForm=

Table[0, {m}, {n}] a zero matrix
Table[Random[ ], {m}, {n}] a matrix with random numerical entries
Table[If[i GreaterEqual j, 1, 0], {i, m}, {j, n}]
a lower-triangular matrix

Constructing special types of matrices with Table.
Table evaluates Random[ ] separately for each element, to give a different pseudorandom number in each case.

In[7]:=  Table[Random[ ], {2}, {2}]

Out[7]=

SparseArray[{}, {n, n}] a zero matrix
SparseArray[{i_, i_} -> 1, {n, n}]
an  identity matrix
SparseArray[{i_, j_} /; i GreaterEqual j -> 1, {n, n}]
a lower-triangular matrix

Constructing special types of matrices with SparseArray.
This sets up a general lower-triangular matrix.

In[8]:=  SparseArray[{i_, j_}/;iGreaterEqualj -> f[i, j], {3, 3}] // MatrixForm

Out[8]//MatrixForm=



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.