Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!… – Szabolcs Apr 25 '13 at 18:52
Strongly related: – R. M. 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
up vote 10 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

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.