# Explain perm in permutedims

I’m trying to understand exactly what perm is in the permutedims function. The docs say: "perm is a vector specifying a permutation of length ndims(A)", but I don’t understand what this means.

To transpose a 2D array, you do permutedims(A, [2,1]), but why is perm equal to [2, 1] in this case? If ndims(A) is equal to 2, what’s the 1 (assuming that the 2 in permutedims(A, [2,1]) is indeed ndims(A))?

I might just be more in need of a Math lesson than a Julia lesson, but I’m really hoping to understand this…

Thanks!!!

1 Like

Consider first transpose. B = transpose(A), which is equivalent to permutedims(A, [2,1]), returns an array B, such that B[i2,i1] == A[i1,i2]. This generalizes to higher dimensions in the following way:

B = permutedims(A, [1,2,3])   #   B[i1,i2,i3] == A[i1,i2,i3]
B = permutedims(A, [1,3,2])   #   B[i1,i3,i2] == A[i1,i2,i3]
B = permutedims(A, [2,1,3])   #   B[i2,i1,i3] == A[i1,i2,i3]
B = permutedims(A, [2,3,1])   #   B[i2,i3,i1] == A[i1,i2,i3]
B = permutedims(A, [3,1,2])   #   B[i3,i1,i2] == A[i1,i2,i3]
B = permutedims(A, [3,2,1])   #   B[i3,i2,i1] == A[i1,i2,i3]


and so on for higher dimensions. So perm is a vector specifying in which order the indices of A are permuted.

3 Likes

The permutedims doc may include your example and/or be more explicit:

Let A be a N-Dim Julia Array with size(A)==(s1,...,sN)

\mbox{Let P=}[p_1,\dots,p_N]~ \mbox{be a permutation of}~[1,\dots,N] \\ \Leftrightarrow \forall i\in [1,N],~~\exists!~ j\in[1,N] \mid p_j=i \\

Then if B=permutedims(A, P) we have:

\forall (i_1,\dots,i_N) \in[1,s_1]\otimes\dots\otimes[1,s_N],~~~~ B_{i_{p_1},\dots,i_{p_N}}=A_{i_1,\dots,i_N}
3 Likes