Mathematica 教程 函数 »|教程 »|更多关于 »

# 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, jth entry Array[f,{m,n}] build an m×n matrix whose i, jth 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, jth entry is a[i, j].
 Out[1]=
Here is another way to produce the same matrix.
 Out[2]=
This creates a 3×2 matrix of zeros.
 Out[3]=
DiagonalMatrix makes a matrix with zeros everywhere except on the leading diagonal.
 Out[4]=
produces an n×n identity matrix.
 Out[5]=
This makes a 3×4 matrix with two nonzero values filled in.
 Out[6]=
MatrixForm prints the matrix in a two-dimensional form.
 Out[7]//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[i≥j, a++, 0] separately for each element, to give a matrix with sequentially increasing entries in the lower-triangular part.
 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 lower-triangular matrix

Constructing special types of matrices with SparseArray.

This sets up a general lower-triangular matrix.
 Out[9]//MatrixForm=