Efficiently reshape an array 2D -> 3D

Viewed 47

I am currently working on datasets where I have 2D matrices / arrays of dimensions (n,pxq). I have to reshape these matrices to obtain a 3D array of size (n,q,p). At the moment, I am stuck with

# Assuming mat2 exists
mat3 = zeros(n,q,p)
for r in 1:n
    mat3[r,:,:] = transpose(reshape(mat2[r,:],p,q))
end

Is there a way to avoid this manipulation, or at least to optimize it a bit ?

1 Answers
Related