# Explain perm in permutedims

**URL:** <https://discourse.julialang.org/t/explain-perm-in-permutedims/19582>\
**Category:** General Usage\
**Created:** [January 13, 2019, 2:09am UTC](https://discourse.julialang.org/t/explain-perm-in-permutedims/19582 "2019-01-13T02:09:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [January 13, 2019, 2:09am UTC](https://discourse.julialang.org/t/explain-perm-in-permutedims/19582/1 "2019-01-13T02:09:23Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![Azamat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/azamat/32/6892_2.png) [@Azamat](https://discourse.julialang.org/u/Azamat)\
**Post date:** [January 13, 2019, 5:27am UTC](https://discourse.julialang.org/t/explain-perm-in-permutedims/19582/2 "2019-01-13T05:27:02Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [January 13, 2019, 8:14am UTC](https://discourse.julialang.org/t/explain-perm-in-permutedims/19582/3 "2019-01-13T08:14:23Z")

</div>

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}
