# Permute Multidimensional Array without allocation

**URL:** <https://discourse.julialang.org/t/permute-multidimensional-array-without-allocation/29153>\
**Category:** General Usage\
**Created:** [September 25, 2019, 9:47am UTC](https://discourse.julialang.org/t/permute-multidimensional-array-without-allocation/29153 "2019-09-25T09:47:10Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 25, 2019, 12:10pm UTC](https://discourse.julialang.org/t/permute-multidimensional-array-without-allocation/29153/9 "2019-09-25T12:10:46Z")

</div>

(Realize that `array[:,i,:] = temp[:,permute,:]` allocates an a extra copy of the `temp[:,permute,:]` slice unless you use `@views`.)

> [@mrVeng](#):
>
> There is a base function `permute!` in Julia but it only works on vector elements, not on whole columns

There actually is an undocumented `Base.permutecols!!(A, p)` function that permutes the whole columns of a matrix `A` by `p` (also overwriting the permutation array `p`). It’s not very long, and you could base something for 3d arrays off it pretty easily: [https://github.com/JuliaLang/julia/blob/99d1d6723d72d43074b5d2765e63d08439989e8e/base/combinatorics.jl#L66-L95](https://github.com/JuliaLang/julia/blob/99d1d6723d72d43074b5d2765e63d08439989e8e/base/combinatorics.jl#L66-L95)

---

_[View the full topic](https://discourse.julialang.org/t/permute-multidimensional-array-without-allocation/29153)._
