Recently I'm spending my time implementing some computer vision algorithms, which usually handle a large amount of data.

The problem I'm facing now is that I have to reform my video data to pass it to Fourier. To be precise, I'm trying to apply a temporal filter to video, whose dimension is Time*X*Y*Channel, while I want to apply Fourier to video[[All, x, y, c]] (x,y,c are some integers).

I've already tried to naively reform the data using Table. It works like charm for small data, but for large data, reforming alone takes long and consumes too much memory. (Took about 4 minutes and 3~4 GB of memory to handle data < 100 MBytes)

MATLAB's shiftdim sounds like a nice solution, while I failed to find an equivalent or workarounds for Mathematica. (Or maybe my approach is wrong?)

In short:

Is there any way to efficiently reform a multi-dimensional array, just like shiftdim?


1 Answer 1


An almost direct equivalent of shiftdim is Transpose with two arguments:

Say, if we have a 3-dimensional array arr, then shiftdim(arr,1) will be the same as

Transpose[arr, {3,1,2}]

More generally,

shiftdim[arr_, k_] := Transpose[arr, RotateRight[Range@ArrayDepth[arr], k]]

is completely equivalent to the MATLAB version of the function for any positive k (MATLAB's shiftdim has a different behaviour for negative k).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.