MATHEMATICA TUTORIAL

# Getting and Setting Pieces of Matrices

 m[[i,j]] the entry m[[i]] the i row m[[All,i]] the i column Take[m,{i0,i1},{j0,j1}] the submatrix with rows through and columns through m[[i0;;i1,j0;;j1]] the submatrix with rows through and columns through m[[{i1,...,ir}, {j1, ... , js}]] the r×s submatrix with elements having row indices and column indices Tr[m,List] elements on the diagonal ArrayRules[m] positions of nonzero elements

Ways to get pieces of matrices.

Matrices in Mathematica are represented as lists of lists. You can use all the standard Mathematica list-manipulation operations on matrices.

Here is a sample 3×3 matrix.
 Out[1]=
This picks out the second row of the matrix.
 Out[2]=
Here is the second column of the matrix.
 Out[3]=
This picks out a submatrix.
 Out[4]=
 m={{a11,a12,...},{a21,a22,...},...} assign m to be a matrix m[[i,j]]=a reset element to be a m[[i]]=a reset all elements in row i to be a m[[i]]={a1,a2,...} reset elements in row i to be m[[i0;;i1]]={v1,v2,...} reset rows through to be vectors m[[All,j]]=a reset all elements in column j to be a m[[All,j]]={a1,a2,...} reset elements in column j to be m[[i0;;i1,j0;;j1]]={{a11,a12,...},{a21,a22,...},...} reset the submatrix with rows through and columns through to new values

Resetting parts of matrices.

Here is a 3×3 matrix.
 Out[5]=
This resets the 2, 2 element to be , then shows the whole matrix.
 Out[6]=
This resets all elements in the second column to be .
 Out[7]=
This separately resets the three elements in the second column.
 Out[8]=
This increments all the values in the second column.
 Out[9]=

A range of indices can be specified by using (Span).

This resets the first two rows to be new vectors.
 Out[10]=
This resets elements in the first and third columns of each row.
 Out[11]=
This resets elements in the first and third columns of rows 2 through 3.
 Out[12]=

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »