Documentation
Mathematica
Built-in Functions
Advanced Documentation
Linear Algebra
Matrix and Tensor Operations

Matrix Permutations
Many matrix techniques rely on ordering a matrix in particular ways. For example, some techniques try to order the matrix to put elements on the diagonal, while others try to group certain elements into dense blocks. The Mathematica function Part is very well suited to applying permutations to the rows and columns of a matrix.

Applying permutations to matrices.
This generates a random matrix.
In[1]:=
Out[2]//MatrixForm=
Now the matrix will be reordered so that the rows are ordered by increasing size of 2-norm. (Norms are discussed in a later section.) First, the norm of each row is computed.
In[3]:=
Out[3]=
This computes the permutation that puts the smallest numbers first.
In[4]:=
Out[4]=
This applies the ordering to the rows of the matrix; the result has rows ordered by increasing size of 2-norm.
In[5]:=
Out[6]//MatrixForm=
Now the inverse permutation is applied by using part assignment. Note that this modifies the matrix held by the symbol pmat. Part assignment is described previously.
In[7]:=
Out[8]//MatrixForm=
If the inverse permutation is applied to the permuted matrix, the original matrix is restored.
In[9]:=
Out[9]=
Permutation Matrices
Another way to work with permutations is to compute a matrix that applies the permutation by multiplication. For example, this builds a 4
4 matrix.
In[1]:=
Out[2]//MatrixForm=
This computes the permutation necessary to order the rows according to ascending 2-norm.
In[3]:=
Out[3]=
The permutation matrix is typically a sparse matrix, which is discussed in a later section. This input generates a sparse identity matrix.
In[4]:=
Out[4]=
If the permutation is applied to the identity matrix, a permutation matrix is generated.
In[5]:=
Out[5]=
This applies the permutation. Note that the rows with smallest norms are at the top.
In[6]:=
Out[7]//MatrixForm=
This applies the inverse permutation.
In[8]:=
Out[8]//MatrixForm=
Typically it is faster to use the Mathematica function Part to apply a permutation, but sometimes it is convenient to work with a permutation matrix.