TITLE: Finding a matrix representation of the transpose transformation
QUESTION [14 upvotes]: Define $T : M_{n×n}(\mathbb{R}) → M_{n×n}(\mathbb{R})$ by $T(A) := A^t$. 
I know this transformation is linear and just takes a matrix and spits out it's transpose. I also know that the transpose is just a matrix with it's columns and rows swapped; however, I don't know how to form a matrix representation of this transformation for arbitrary $n$. 
Any help to get me started would be appreciated!

REPLY [2 votes]: Here is another way to think about it.
Consider a square matrix $\mathbf{A}$ of dimension $d$ for simplicity. Then consider the standard basis of $d$ column unit vectors $\mathbf{e}_i$ with elements $(\mathbf{e}_i)_j = \delta_{ij}$. These are just column vectors with a single element set to $1$ and all other elements set to zero.
A matrix $\mathbf{A}$ with elements $a_{ij}$ can always be decomposed using the basis vectors as:
$$
\mathbf{A} = \sum_{ij} a_{ij}\,\mathbf{e}_i \mathbf{e}_j^T.
$$
In particular, the matrix $\mathbf{A}^T$ may be written as
$$
\mathbf{A}^T = \sum_{ij} a_{ji}\,\mathbf{e}_i \mathbf{e}_j^T.
$$
The elements $a_{ji}$ of $\mathbf{A}^T$ can also be expressed in terms of the unit vectors:
$$
a_{ji} = \mathbf{e}_j^T \mathbf{A} \mathbf{e}_i.
$$
Therefore,
$$
\mathbf{A}^T = \sum_{ij} \left(\mathbf{e}_j^T \mathbf{A} \mathbf{e}_i\right)\,\mathbf{e}_i \mathbf{e}_j^T = \sum_{ij} \left(\mathbf{e}_i \mathbf{e}_j^T\right) \mathbf{A} \left(\mathbf{e}_i \mathbf{e}_j^T \right).
$$
If we relabel the matrices $\mathbf{e}_i \mathbf{e}_j^T$ as $\mathbf{u}_k$, with $k = 1,\dots,d^2$, then we can write
$$
\mathbf{A}^T = \sum_{k} \mathbf{u}_k \mathbf{A} \mathbf{u}_k.
$$
The matrices $\mathbf{u}_k$ are just all matrices containing only one non-zero element equal to $1$.
The above expression is the expression for the linear transposition map in the original matrix space. Here, we don't need to reshape the matrix in vector form. We can simply multiply the matrix by the unit matrices $\mathbf{u}_k$ on the left and on the right.
It can then be shown that if the elements of the matrix $\mathbf{A}$ are ordered by column in standard order, $\mathcal{A} = \left(a_{11},a_{21},a_{31},\dots,a_{12},a_{22},a_{32},\dots\right)^T$, the linear transposition map takes the form
$$
\mathcal{A}^T = \mathcal{T}\mathcal{A},
$$
with
$$
\mathcal{T} = \sum_k \mathbf{u}_k^T \otimes \mathbf{u}_k.
$$
Here, $\otimes$ is the Kronecker product as defined by, e.g., Matlab's kron( ) function. For $d=2$, we have
$$
\begin{align}
\mathcal{T} =
\left(
\begin{array}{cccc}
1 & 0 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 0 & 1
\end{array}
\right)
\end{align}
$$
For $d = 3$, we have
$$
\begin{align}
\mathcal{T} =
\left(
\begin{array}{ccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
\end{array}
\right).
\end{align}
$$
For $d=4$, we have
$$
\begin{align}
\mathcal{T} = 
\left(
\begin{array}{cccccccccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \\
\end{array}
\right),
\end{align}
$$
and so on.
Notice that the transposition map is a permutation of the columns (or rows) of the identity matrix. The permutation is the permutation that groups the columns by jumps of $d$. The first four columns are columns $1,d+1,2d+1,\dots, (d-1)d+1$ of the identity matrix. The next four columns are columns $2,d+2,2d+2,\dots, (d-1)d+2$ of the identity matrix. And it goes like this in groups of $d$.
Here is the Matlab code that will give you the matrix for any dimension:
% Set the dimension of A
d = 4;
% Calculate the permutation of the columns of the identity operator
perm = (1:d^2).';
perm = reshape(perm,d,d).';
perm = perm(:).';
% Calculate the linear transposition map
T = eye(d^2);
T = T(:,perm);