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.

I have a multidimensional array A. Fourier[A] finds the discrete Fourier transform over all dimensions. How to find discrete Fourier transform over a custom dimension like fft(A,[],dim) function in MATLAB?

My current solution:

FourierDim[list_, dim_] := 
 Module[{dims, ord, depth = ArrayDepth[list], tr, f},
  dims = If[IntegerQ[dim], If[dim > 0, {dim}, {1 + depth + dim}], dim];
  ord = Join[Complement[Range[depth], dims], dims];
  tr = Transpose[list, Ordering[ord]];
  f = ConstantArray[0.0 + 0.0 I, Dimensions[tr]];
  With[{nn = Sequence @@ Table[Unique["n"], {depth - Length@dims}]}, 
   With[{lim = Sequence @@ ({{nn}, Dimensions[tr][[1 ;; -1 - Length@dims]]}\[Transpose])}, 
    Do[f[[nn]] = Fourier@tr[[nn]], lim]]];
  Transpose[f, ord]

It transposes the array, does Fourier transform in the loop, and transposes the array backward.


FourierDim[A,2]; (* Fourier over the second dimension *)
FourierDim[A,-1]; (* Fourier over the last dimension *)
FourierDim[A,{1,3}]; (* Fourier over the first and the third dimensions *)
share|improve this question
I don't understand what the desired output is. Say you had a n1xn2 matrix of values -- is it that FourierDim[A,1] would be the 1-D FFT of each row and FourierDim[A,2] would be the 1-D FFT of each column? –  bill s Sep 4 '13 at 17:48
@bills Yes but vice versa: FourierDim[A,1] would be the 1-D FFT of each column and FourierDim[A,2] would be the 1-D FFT of each row. For example, FourierDim[A,1][[All,1]]==Fourier[A[[All,1]]] and FourierDim[A,2][[1,All]]==Fourier[A[[1,All]]]. –  ybeltukov Sep 4 '13 at 19:17

Your Answer


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

Browse other questions tagged or ask your own question.