This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / The Mathematica Book / Advanced Mathematics in Mathematica / Linear Algebra /

3.7.1 Constructing Matrices

Functions for constructing matrices.

This generates a matrix whose entry is a[i, j].

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


Here is another way to produce the same matrix.

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


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

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


IdentityMatrix[n] produces an identity matrix.

In[4]:= IdentityMatrix[3]


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

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


MatrixForm prints the matrix in a two-dimensional form.

In[6]:= MatrixForm[%]


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


Constructing special types of matrices with SparseArray.

This sets up a general lower-triangular matrix.

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