 |  |
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. | Here is another way to produce the same matrix. | |
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. | |
Out[6]//MatrixForm=
|
|
| Table[0, {m}, {n}] | a zero matrix | | Table[Random[ ], {m}, {n}] | a matrix with random numerical entries |
Table[If[i 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]=
|
|
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
|
Out[8]//MatrixForm=
|
|
|
|
|