Matlab and R functions for fft(): N-point FFT and dimension issue In MATLAB, Y = fft(X,n,dim) returns the Fourier transform along the dimension dim. For example, if X is a matrix, then fft(X,n,2) returns the n-point Fourier transform of each row. However, in R is different. Do you know any function to solve this; so that takes into account n and dim, to get a n length complex vector? See that in the example that n&gt;ncol(X).
MATLAB code
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector

x1 = cos(2*pi*50*t); % First row wave
x2 = cos(2*pi*150*t); % Second row wave
x3 = cos(2*pi*300*t);  % Third row wave

n = 2^nextpow2(L);
dim = 2;
X = [x1; x2; x3];

Y_right = fft(X,n,dim); %Result: 3*1024 complex double


 Y_wrong = fft(X); %Result: 3*1000 complex double (I am looking for the above result)


R code
Fs &lt;- 1000
T &lt;- 1/Fs
L &lt;- 1000
t &lt;- (0:(L-1))*T

x1 &lt;- cos(2*pi*50*t)
x2 &lt;- cos(2*pi*150*t)
x3 &lt;- cos(2*pi*300*t)

n&lt;-2^ceiling(log2(abs(L)))
dim &lt;- 2
X &lt;- t(matrix(c(x1, x2, x3), ncol=3))

Y &lt;- fft(X) 
# Result: 3*1000 cplx and it is not the same as `Y_wrong` (operates along each row) 
#To get Y_wrong:

Y &lt;- matrix(0, nrow=3, ncol=1000)

for (i in 1:ncol(X)) {
Y[,i] &lt;- fft(X[,i], inverse = T)
}


My aim is to get Y_right, and Y think that by using apply we get the same as Y_wrong. Thank you in advance.
R allows you to loop over rows or columns through the use of the apply family of functions. Note that apply is not necessarily faster than looping but more in line with the functional programming nature of R. There are other apply functions such as lapply, vapply, sapply etc but for your purposes, apply should suffice.  Also, note that you may have to transpose the result using t(result). 1 means go over rows and 2 means go over columns. So, if X is the matrix, 

apply(X, 1, fft) does the fft of the rows.

apply(X, 2, fft) does the fft of the columns.


Note that the use of .row or .col is my convention. I could have used x, y , or z because that's just a dummy variable from apply's point of view.

Also, you can type ? apply in R in order to obtain the help for apply. Sometimes they can be kind of obscure but atleast they tell you what arguments are available.