此为 Mathematica 7 文档,内容基于更早版本的 Wolfram 语言
查看最新文档(版本11.1)
Mathematica HowTo
How to | Get Parts of a Matrix
Mathematica has many matrix operations that support operations such as building, computing, and visualizing matrices. It also has a rich language for picking out and extracting parts of matrices.
Define the following matrix. Note how a matrix in Mathematica is not restricted to number entries:
In[1]:=
Click for copyable input
Out[1]//MatrixForm=
Use Part to go to the first row and take the second element:
In[2]:=
Click for copyable input
Out[2]=
Or use the shorthand notation [[...]] for Part:
In[3]:=
Click for copyable input
Out[3]=
To take an entire row, use one index to specify the row:
In[4]:=
Click for copyable input
Out[4]=
To take an entire column, you need to select all rows with All and specify the column:
In[5]:=
Click for copyable input
Out[5]=
    
To pick out a submatrix you can use Span (;;). First define a 4×5 matrix:
In[6]:=
Click for copyable input
Out[6]//MatrixForm=
The top-left 3×4 matrix with bold entries here corresponds to rows 1 through 3 and columns 1 through 4:
Out[7]//MatrixForm=
Extract the highlighted matrix by using the short form ;; of Span to specify the relevant span of rows and columns:
In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]//MatrixForm=
Extract all elements except the outermost rows and columns (negative indices count from the end):
In[10]:=
Click for copyable input
Out[10]=
In[11]:=
Click for copyable input
Out[11]//MatrixForm=