# How to aplly a function to all columns?

3 views (last 30 days)
Gabriel Aguirre on 22 Jan 2023
Answered: dpb on 22 Jan 2023
Hi guys, i have a code which do fft, however are so many columns to apply this code i need a way to do it faster, i already see something about cellfun, but this did't work. my data comes from vibration, I have a column of time and the rest of vibration data.
the following function makes the fft, psd now i want to apply to all columns of an excel table (csv).
function [freq_ax,fft_ax, psd_ax] = my_fft(t,ax)
%% computando fft
dt = mean(diff(t));
n = length(t);
L = 1:floor(n/2);
freq = 1/(dt*n)*(0:n-1);
fhat = (fft(ax,n)*1/(n-1));
fft_ax = abs(fhat(L));
freq_ax = (freq(L));
psd = fhat.*conj(fhat);
psd_ax = psd(L);
end

dpb on 22 Jan 2023
See fft -- in particular, note the first comment...
"fft(X) is the discrete Fourier transform (DFT) of vector X. For matrices, the fft operation is applied to each column."
Hence, you get the operations applied to all columns for free; just pass the vibration data array (without augmenting it with the time which is of no import for the FFT, all you need is the sample rate) to FFT and operate by column for the rest. You'll get N PSDs; one per column.