Constructing Matrices

Table[f,{i,m},{j,n}]build an m×n matrix where f is a function of i and j that gives the value of the ^(th) entry
Array[f,{m,n}]build an m×n matrix whose ^(th) entry is
ConstantArray[a,{m,n}]build an m×n matrix with all entries equal to a
DiagonalMatrix[list]generate a diagonal matrix with the elements of list on the diagonal
IdentityMatrix[n]generate an n×n identity matrix
Normal[SparseArray[{{i1,j1}->v1,{i2,j2}->v2,},{m,n}]]make a matrix with nonzero values at positions

Functions for constructing matrices.

This generates a 2×2 matrix whose ^(th) entry is .
In[1]:=
Click for copyable input
Out[1]=
Here is another way to produce the same matrix.
In[2]:=
Click for copyable input
Out[2]=
This creates a 3×2 matrix of zeros.
In[3]:=
Click for copyable input
Out[3]=
DiagonalMatrix makes a matrix with zeros everywhere except on the leading diagonal.
In[4]:=
Click for copyable input
Out[4]=
IdentityMatrix[n] produces an n×n identity matrix.
In[5]:=
Click for copyable input
Out[5]=
This makes a 3×4 matrix with two nonzero values filled in.
In[6]:=
Click for copyable input
Out[6]=
MatrixForm prints the matrix in a twodimensional form.
In[7]:=
Click for copyable input
Out[7]//MatrixForm=
Table[0,{m},{n}]a matrix of zeros
Table[If[i>=j,1,0],{i,m},{j,n}]a lowertriangular matrix
RandomReal[{0,1},{m,n}]a matrix with random numerical entries

Constructing special types of matrices.

Table evaluates If[ij,a++,0] separately for each element, to give a matrix with sequentially increasing entries in the lower-triangular part.
In[8]:=
Click for copyable input
Out[8]=
SparseArray[{},{n,n}]a zero matrix
SparseArray[{i_,i_}->1,{n,n}]an n×n identity matrix
SparseArray[{i_,j_}/;i>=j->1,{n,n}]a lowertriangular matrix

Constructing special types of matrices with SparseArray.

This sets up a general lowertriangular matrix.
In[9]:=
Click for copyable input
Out[9]//MatrixForm=