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[267]:=
Click for copyable input
Out[268]//MatrixForm=

Use Part to go to the first row and take the second element:

In[269]:=
Click for copyable input
Out[269]=

Or use the shorthand notation for Part:

In[270]:=
Click for copyable input
Out[270]=

To take an entire row, use one index to specify the row:

In[271]:=
Click for copyable input
Out[271]=

To take an entire column, you need to select all rows with All and specify the column:

In[272]:=
Click for copyable input
Out[272]=
    

To pick out a submatrix, you can use Span (). First, define a 4×5 matrix:

In[273]:=
Click for copyable input
Out[274]//MatrixForm=

The top-left 3×4 matrix with bold entries here corresponds to rows 1 through 3 and columns 1 through 4:

Out[274]//MatrixForm=
Click for copyable input

Extract the highlighted matrix by using Span () to specify the relevant span of rows and columns:

In[275]:=
Click for copyable input
Out[275]=
In[276]:=
Click for copyable input
Out[276]//MatrixForm=

Extract all elements except the outermost rows and columns (negative indices count from the end):

In[277]:=
Click for copyable input
Out[277]=
In[278]:=
Click for copyable input
Out[278]//MatrixForm=
New to Mathematica? Find your learning path »
Have a question? Ask support »