TITLE: How do you transpose tensors?
QUESTION [11 upvotes]: We transpose a matrix $A$ by replacing $A_{ij}$ with $A_{ji}$, for all $i$ and $j$.
However, in case $A$ has more than two dimensions (that is, it is a tensor), I don't know how to apply the transpose operation.

If A has dimensions $3\times 3 \times 8$, then what will replace $A_{ijk}$? 
If $A$ has shape $3\times 3\times 8\times 8$, then what will replace $A_{ijkl}$?

REPLY [2 votes]: Here is an example for 3D array,
First decide if it's row major (e.g. C) or column major (e.g. Fortran).
For a C array A[n3][n2][n1], the fastest dimension is n1, and we can name this original sequence as 321.
Then we can say we want a 321 -> 132 transpose operation, which is to copy the array element A[i3][i2][i1] to B[i1][i3][i2].
Often we assume from fastest dimension to slowest dimension being 123, then we can call the above transpose operation as 231 (i.e., 123->231).