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 *)
  • 1
    $\begingroup$ 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? $\endgroup$
    – bill s
    Sep 4, 2013 at 17:48
  • $\begingroup$ @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]]]. $\endgroup$
    – ybeltukov
    Sep 4, 2013 at 19:17

1 Answer 1


Move the dimension you want to FT to the lowest level using Transpose, FT there using Map, then Transpose back. For example, if you want to only transform the top level of a 3D array, then do this:

F[h_]:=Transpose[Map[Fourier, Transpose[h, {3, 2, 1}], {2}], {3, 2, 1}]

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.