# How do I change the dimension of an array

**URL:** <https://discourse.julialang.org/t/how-do-i-change-the-dimension-of-an-array/107623>\
**Category:** General Usage\
**Tags:** question, arrays\
**Created:** [December 14, 2023, 5:25pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-dimension-of-an-array/107623 "2023-12-14T17:25:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)\
**Post date:** [December 14, 2023, 5:25pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-dimension-of-an-array/107623/1 "2023-12-14T17:25:08Z")

</div>

If I have an array with the size of 33x180x360, how do I change it to the size of 33x360x180?

Thank you.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [December 14, 2023, 5:37pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-dimension-of-an-array/107623/2 "2023-12-14T17:37:14Z")

</div>

If the order of elements remains unchanged, you may use `reshape`:

```julia
julia> A = [i+j+k for i in 1:4, j in 1:3, k in 1:2]
4×3×2 Array{Int64, 3}:
[:, :, 1] =
 3 4 5
 4 5 6
 5 6 7
 6 7 8

[:, :, 2] =
 4 5 6
 5 6 7
 6 7 8
 7 8 9

julia> reshape(A, 3, 4, 2)
3×4×2 Array{Int64, 3}:
[:, :, 1] =
 3 6 6 6
 4 4 7 7
 5 5 5 8

[:, :, 2] =
 4 7 7 7
 5 5 8 8
 6 6 6 9

```

On the other hand, if you’re trying to permute the last two axes, you may use `permutedims`:

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

[:, :, 2] =
 4 5
 5 6
 6 7
 7 8

[:, :, 3] =
 5 6
 6 7
 7 8
 8 9

```

---

<div class="post-metadata">

**Author:** ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)\
**Post date:** [December 14, 2023, 5:39pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-dimension-of-an-array/107623/3 "2023-12-14T17:39:26Z")

</div>

> [@jishnub](#):
>
> `permutedims(A, (1, 3, 2))`

I think permutedims is what I need. Many thanks!

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 14, 2023, 5:47pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-dimension-of-an-array/107623/4 "2023-12-14T17:47:25Z")

</div>

Another suggestion would be to have a good look at `Images` package eco-system. These packages would be tackling a lot of the stuff you seem to be on and have done some nice work on it.

For example, for the question in the post look at:

```julia
julia> using Images

help?> PermutedDimsArray
search: PermutedDimsArray permuteddimsview

  PermutedDimsArray(A, perm) -> B

  Given an AbstractArray A, create a view B such that the dimensions appear to be permuted. Similar
  to permutedims, except that no copying occurs (B shares storage with A).

  See also permutedims, invperm.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> A = rand(3,5,4);
  
  julia> B = PermutedDimsArray(A, (3,1,2));
  
  julia> size(B)
  (4, 3, 5)
  
  julia> B[3,1,2] == A[1,2,3]
  true

```
