# Getting and Setting Pieces of Matrices

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

Ways to get pieces of matrices.

Matrices in the Wolfram Language are represented as lists of lists. You can use all the standard Wolfram Language listmanipulation operations on matrices.

Here is a sample 3×3 matrix:
 In:= Out= This picks out the second row of the matrix:
 In:= Out= Here is the second column of the matrix:
 In:= Out= This picks out a submatrix:
 In:= Out= m={{a11,a12,…},{a21,a22,…},…} assign m to be a matrix m[[i,j]]=a reset element {i,j} 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 {a1,a2,…} m[[i0;;i1]]={v1,v2,…} reset rows i0 through i1 to be vectors {v1,v2,…} 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 {a1,a2,…} m[[i0;;i1,j0;;j1]]={{a11,a12,…},{a21,a22,…},…} reset the submatrix with rows i0 through i1 and columns j0 through j1 to new values

Resetting parts of matrices.

Here is a 3×3 matrix:
 In:= Out= This resets the 2, 2 element to be x, then shows the whole matrix:
 In:= Out= This resets all elements in the second column to be z:
 In:= Out= This separately resets the three elements in the second column:
 In:= Out= This increments all the values in the second column:
 In:= Out= A range of indices can be specified by using ;; (Span).

This resets the first two rows to be new vectors:
 In:= Out= This resets elements in the first and third columns of each row:
 In:= Out= This resets elements in the first and third columns of rows 2 through 3:
 In:= Out= 