Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
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
add comment

1 Answer

up vote 9 down vote accepted

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

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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