10

It is very likely that it is my lack of math skills that is showing up here. However, I think Transpose is such an important function that I need to master it. So I am going to ask because I do not understand the information given in the documentation.

Transpose[list,{Subscript[n, 1], Subscript[n, 2], …}] transposes list so that the k-th level in list is the Subscript[n, k]-th level in the result.

Would anyone be able to provide a simple example and point out in a matrix what is going on? I am in particular struggling with the nth and k-th level. How does that work?

I want to master Transpose[list, {…}]. Please explain the case where the k-th and n-th level are to be transposed, since this is where I am struggling.

If this is considered mathematical question, and therefore not in the right place, I would appreciate a comment so that I could delete the post.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
ALEXANDER
  • 1,219
  • 8
  • 21

2 Answers2

16

Here is a visualization of the 3 dimensional case. A part of the tensor is indexed by

`tensor[[l1, l2, l3]]`

where l1, l2, l3 are the indices to levels 1, 2, 3 respectively. Transposing switches how the values are indexed. For example, if new = Transpose[old, {2, 3, 1}], then new[[l3, l1, l2]] == old[[l1, l2, l3]] or new[[l1, l2, l3]] == old[[l2, l3, l1]]. The first equality corresponds to how the result is described in Transpose.

In the visualization below, the colors are transposed according to the permutation labeling the graphics. Level 1 corresponds to hue, level 2 to saturation, and level 3 to brightness. The upper left is the identity permutation and corresponds to the original tensor. The labels on the axes correspond to the level in the original tensor.

tensor = Table[{i, j, k}, {i, 4}, {j, 4}, {k, 4}];
cf[i_, j_, k_] := Hue[(i - 1)/4, j/4, (k + 3)/9];
g[p_] := Graphics3D[{
    PointSize[0.1],
    Point[Flatten[tensor, 2], 
     VertexColors -> cf @@@ Flatten[Transpose[tensor, p], 2]]
    },
   PlotRange -> {{0.8, 4.2}, {0.8, 4.2}, {0.8, 4.2}},
   PlotLabel -> p,
   Axes -> True, Ticks -> None,
   AxesLabel -> Ordering@p
   ];

GraphicsGrid[Partition[Table[g[p], {p, Permutations[{1, 2, 3}]}], 3]]

Mathematica graphics

I hope that this example will help with understanding how higher dimensional tensors are transposed.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Thanks. That's very helpful. In your example, new[[l3, l1, l2]] == old[[l1, l2, l3]] or new[[l1, l2, l3]] == old[[l2, l3, l1]] , do you have an easy way to know the arrangement {l2,l3,l1} given {l3,l1,l2}? For example, I have a tensor with dimension {l1,l2,l3,l4}, and I want to have new[[l2, l3, l1,l4]] == old[[l1, l2, l3,l4]], what should I put in Transpose[old, ]? – xslittlegrass Sep 17 '15 at 05:01
  • 1
    @xslittlegrass, It's a question of permuting the levels. For your second question FindPermutation[{l1, l2, l3, l4}, {l2, l3, l1, l4}] // PermutationList[#, 4] & will return what you need. For the first, in {l3,l1,l2}, l1 went to position 2; so the permutation start with {2,..}; next l2 went to position 3, so the permutation becomes {2, 3,...}; finally, l3 went to position 1, so the argument to Transpose is/was {2, 3, 1}. – Michael E2 Sep 17 '15 at 10:47
2

This is more of a math question, but in the spirit of being helpful:

I think if you run this code and look at the colors of each matrix you might understand better what transpose does.

    m = Table[Graphics[{RGBColor[0, .33 i, .33 j], Disk[]}], {i, 1, 3},
     {j,1,3}] // MatrixForm;
    mT = Transpose@Table[Graphics[{RGBColor[0, .33 i, .33 j], Disk[]}],
     {i, 1, 3}, {j, 1, 3}] // MatrixForm;
    Row[{m, mT}]

Transpose basically reflects elements across the diagonal.

Notice how the first column of the original matrix is the same as the first row of the transposed matrix.

bshev
  • 383
  • 2
  • 9
  • 4
    I think the OP had in mind the syntax with the second argument of Transpose, which allows complex array reshuffles for arrays of higher dimensions, rather than the (trivial) single-arg Transpose. – Leonid Shifrin Apr 13 '14 at 02:32