Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm having difficulty understanding the Transpose function. I know what the transpose of a matrix is, not a problem, and I see that applying the Transpose function to a 3×3 matrix does what is expected.

What I am having difficulty with is the definition, which says:

Transpose[list], "transpose the top two levels of list"

Consider:

A = {{a, b}, {c, d}}
Transpose[A]

What does it mean when the definition says "transpose the top two levels of the list?"

I've read about levels in lists, experimented with TreeForm, but something like this is giving me a lot of trouble.

A = {{{a, b}, {c, d}}, {{e, f}, {g, h}}, {{i, j}, {k, l}}}
Transpose[A, {3, 1, 2}]

Can someone help me (gently) understand the definition:

Transpose[list,{n1,n2,...}], "put the $k^{th}$ level in list at level $n_k$" ?

share|improve this question
2  
So as to not replicate the effort, please also read this answer by Leonid, which is for Flatten with non-trivial second arguments. The idea is the same for Transpose, but the direction is reversed (i.e., it's an "inverse" operation) –  rm -rf Dec 26 '12 at 17:41

1 Answer 1

up vote 19 down vote accepted

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$"

share|improve this answer
1  
+1 I am thinking the illustrations might work even better with some color coding. E.g., colors[1, 1] = Darker[Red]; colors[1, 2] = Darker[Green]; colors[1, 3] = Darker[Blue]; colors[2, 1] = Lighter[Red]; colors[2, 2] = Lighter[Green]; colors[2, 3] = Lighter[Blue]; f[x_] := Replace[MatrixForm[x], Subscript[a, i_, j_, k___] -> Style[Subscript[a, i, j, k], colors[i, j]], -1]; f[A], etc. –  whuber Dec 26 '12 at 18:35
1  
Yeah! Something like this preschoolteachingaidsbycarolyn.com/images/Froggy%20Sums.jpg –  belisarius Dec 26 '12 at 19:33
    
@belisarius Shouldn't it be toads instead of frogs for this answerer? –  Jens Dec 26 '12 at 22:17
1  
@Jens I'm as bad in Zoology as in Math. –  belisarius Dec 26 '12 at 22:21
1  
@whuber Thanks, I've added some colour coding to the elements :) –  rm -rf Dec 27 '12 at 0:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.