# understanding FFT2 function of Matlab

I am trying to understand what happen when we take FFT2 of a matrix in a matlab.

first have a look at this simple example,

a=ones(8); %8x8 Matrix of Ones
fft2(a)


it will generate the following output,

    64     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0


in above matrix , 64 is the sum of all the matrix values and it is the DC coefficient. Does that mean the 64 is calculated as 8*8 = 64 ?

Now the actual question, i generated a matrix of 4,4,

   a = [zeros(4,2) ones(4,2)]

% 0     0     1     1
% 0     0     1     1
% 0     0     1     1
% 0     0     1     1


First 4 rows and Two columns are zeros while the remaining are ones as shows above.

Now if i take its FFT2,

    fft2(a)


It generates the following output,

  8.0000            -4.0000 + 4.0000i         0            -4.0000 - 4.0000i
0                  0                  0                  0
0                  0                  0                  0
0                  0                  0                  0


According to the book, the values shown in the output are the DC coefficient and it is indeed the sum of all the matrix values.

In previous example it was easy to calculate the DC coefficient but in this example how these values are generated ?

  8.0000            -4.0000 + 4.0000i         0            -4.0000 - 4.0000i


If i calculate the fft instead of fft2 it will give the following output

%FFT -> DFT of Vector
%FFT2 -> DFT of Matrix
0     0     4     4
0     0     0     0
0     0     0     0
0     0     0     0


The fft() function calculates the one-dimensional FFT of its input argument. If the input argument is a vector, then the operation is pretty simple to understand; the output is just the result of efficiently calculating a discrete Fourier transform on the input. If the input is a matrix, then, as many MATLAB functions do, each column is treated as a separate one-dimensional signal and are transformed separately. Therefore, the $n$-th column of the output matrix corresponds to the DFT of the $n$-th column of the input matrix.
The fft2() function calculates the two-dimensional FFT of a matrix. One way of envisioning this operation is to take standard one-dimensional FFTs down each column of the input matrix $A$ to yield a temporary result $B$. Then, take one-dimensional FFTs across each row of $B$ to yield the result $C$. The matrix $C$ would then be the result of the two-dimensional FFT of A. 2-D transforms (although not typically the FFT) are often used in image and video compression algorithms.