Let's look at this with a simple example without considering complicated indexing and levels. Consider the list (the colours are merely for visual guidance):
A = Array[Subscript[a, ##] &, {2, 3, 4}]
Dimensions@A
(* {2, 3, 4} *)
This is a list containing 2 lists, each of which contains 3 sublists, which in turn contain 4 elements of the array.
What does "top two levels" mean?
If you consider the 2 rows above as "level 1" (not related to Level
) or the first dimension (this is a better word than "level"), the 3 columns as "level 2" or the second dimension and the 4 elements as "level 3" and so on, what the transpose operation does on this multi-dimensional array is to simply flip the rows and columns (i.e., levels 1 and 2), just like in a matrix.
Atrans = Transpose[A]
Dimensions@Atrans
(* {3, 2, 4} *)
The only difference is that in a matrix, the $(i,j)^{th}$ element is a single element, whereas here it is a list of 4 elements. Calling Dimensions
on the above two arrays will show you that it is flipped from {2,3,4}
to {3,2,4}
(the third dimension is left intact).
How do you use the second argument of Transpose
?
The statement "transpose the first two levels" becomes a little clearer if you consider an explicit second argument for Transpose
:
Atrans == Transpose[A, {2, 1}]
(* True *)
So you see that we simply swapped dimensions 2 and 1. This would then mean that if I used {1, 2}
as the second argument, the array should be left intact:
A == Transpose[A, {1, 2}]
(* True *)
The second argument is useful when you want to do more complex swaps, like for example, $1\to3, 2\to1, 3\to 2$, which is easily expressed as:
Transpose[A, {3, 1, 2}]
Dimensions@%
(* {3, 4, 2} *)
In other words, the 1st dimension in A
is now the 3rd dimension in the result, the 2nd is now the 1st and the 3rd is now the 2nd, which is what is meant by "put the $k$th level in list at level $n_k$"
Flatten
with non-trivial second arguments. The idea is the same forTranspose
, but the direction is reversed (i.e., it's an "inverse" operation) $\endgroup$