Permute Multidimensional Array without allocation

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

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

4 Likes