2

I have a problem with calculating fft for a signal which is stored in a matrix using matlab. I am trying to calculate fft for each column.

I am trying to do this like this:

 for k = 1: ncol
    y1(k)= fft(y(:,k));
 end

where y is my matrix and and ncol is number of columns in matrix but I'm still getting the following error:

In an assignment  A(:) = B, the number of elements in A and B must be the same.
SleuthEye
  • 14,379
  • 2
  • 32
  • 61
Ewa
  • 65
  • 3
  • 11

2 Answers2

4

Just do this

y1 = fft(y);

It computes each column separately and it does it much faster than using a for loop.

In response to your original question, you would have to do it like this:

 for k = 1: ncol
    y1(:,k)= fft(y(:,k));
 end

You were trying to put an entire column into a single index which is why you were getting that error message. You need to allocate more room so that the entire column can be stored.

Durkee
  • 778
  • 6
  • 14
  • 2
    It is also worth nothing that `fft` also takes an optional argument to explicitly specify the dimension along which the FFT is computer. So `fft(y,[],1)` computes each column separately, and `fft(y,[],2)` computes each row separately. – SleuthEye Sep 09 '18 at 02:29
0

y1 should also be in matrix form. fft of a signal is an array of coefficients

YoniChechik
  • 1,397
  • 16
  • 25