# 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 i,j entry Array[f,{m,n}] build an m×n matrix whose i,j entry is f[i,j] 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 vk at positions {ik,jk}

Functions for constructing matrices.

This generates a 2×2 matrix whose i,j entry is a[i,j]:
 In:= Out= Here is another way to produce the same matrix:
 In:= Out= This creates a 3×2 matrix of zeros:
 In:= Out= DiagonalMatrix makes a matrix with zeros everywhere except on the leading diagonal:
 In:= Out= produces an n×n identity matrix:
 In:= Out= This makes a 3×4 matrix with two nonzero values filled in:
 In:= Out= MatrixForm prints the matrix in a twodimensional form:
 In:= Out//MatrixForm= Table[0,{m},{n}] a matrix of zeros Table[If[i>=j,1,0],{i,m},{j,n}] a lower‐triangular 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:= Out= 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 lower‐triangular matrix

Constructing special types of matrices with SparseArray.

This sets up a general lowertriangular matrix:
 In:= Out//MatrixForm= 