# Is there an equivalent of “shiftdim” of MATLAB?

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?

-
Also make sure that your array is packed to reduce memory usage and increase performance! mathematica.stackexchange.com/questions/3496/… –  Szabolcs Apr 25 '13 at 18:52
Strongly related: mathematica.stackexchange.com/q/16968/5 –  rm -rf Apr 25 '13 at 19:00
Thanks for the great advices. Now my implementation does the job in 70 seconds using less than 2 GB of memory! –  Eon Apr 25 '13 at 20:07

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).

-