# How to transpose a 3d array?

**URL:** <https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311>\
**Category:** New to Julia\
**Tags:** array, arrays\
**Created:** [July 26, 2021, 3:14pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311 "2021-07-26T15:14:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [July 26, 2021, 3:14pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311/1 "2021-07-26T15:14:00Z")

</div>

Hi,

Is there an elegant way to transpose the first and second dimensions of a 3d array? For example, I’d like to go from an NxMxP array to an MxNxP array.

I’ve read the documentation etc., but can’t seem to extrapolate the 2d examples provided into a 3d format.

Cheers!

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [July 26, 2021, 3:19pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311/2 "2021-07-26T15:19:27Z")

</div>

Try permutedims or permutedims!

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [July 26, 2021, 3:35pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311/3 "2021-07-26T15:35:05Z")

</div>

Hey @Paulo_Jabardo, thanks for the solution! I tried two methods (Examples below) both return a vector of matrices. How do I convert that vector of matrices into a 3d array? Thanks!

```julia
#method 1
result1 = transpose.(array[:,:,i] for i = 1:11)
#method2
result2 = permutedims.(array[:,:,i] for i = 1:2)

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 26, 2021, 3:38pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311/4 "2021-07-26T15:38:56Z")

</div>

You don’t want to broadcast the call to `permutedims` here, you just need to call the function directly:

```julia
julia> x = reshape(1:12, (2, 3, 2))
2×3×2 reshape(::UnitRange{Int64}, 2, 3, 2) with eltype Int64:
[:, :, 1] =
 1 3 5
 2 4 6

[:, :, 2] =
 7 9 11
 8 10 12

julia> permutedims(x, (2, 1, 3))
3×2×2 Array{Int64, 3}:
[:, :, 1] =
 1 2
 3 4
 5 6

[:, :, 2] =
  7 8
  9 10
 11 12

```

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [July 26, 2021, 3:56pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311/6 "2021-07-26T15:56:18Z")

</div>

Thanks @rdeits , I’m sorry, I couldn’t find in the documentation where it details what ,(2,1,3) represents. Will you please point me to where I can find it? Thank you!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [July 26, 2021, 4:36pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311/7 "2021-07-26T16:36:48Z")

</div>

(2,1,3) is the permutation. It means move 2 to 1, 1 to 2, and leave 3 at 3.

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [July 26, 2021, 4:47pm UTC](https://discourse.julialang.org/t/how-to-transpose-a-3d-array/65311/8 "2021-07-26T16:47:32Z")

</div>

Legendary, got it!
