MATLAB Answers

Skipping a NaN in a loop

6 ビュー (過去 30 日間)
Ryan Hughes
Ryan Hughes 2021 年 4 月 9 日
回答済み: KSSV 2021 年 4 月 9 日
Hi guys, currently doing a project for my module at uni and we are having to get averages of amplitude from seismic data from certain stations. Each file contains data from one station, with an array called 'seis'. Each column in the array represents a day, however some columns have NaN values (for some reason the seismometer wasn't recording). I can't edit the NaN or else it will throw off amplitude average, so I just want to skip that day (column) in my loop, however I cant seem to figure it out as every time I run the loop I get an error code saying 'Error using 'periodogram', expected x to be finite'
I know I wll probably need to use an 'if' statement, however I dont what to put in so that it skips the NaN columns. Here is the code:
Daily_mean_seis=[];
for i=1:1:240
[amp, freq] = periodogram(detrend(seis(:,i)),[],[],Fs);
F_freq=find(freq>=4&freq<=14);
F_amp=amp(F_freq);
av_amp=mean(F_amp);
Daily_mean_seis=[Daily_mean_seis;av_amp];
end
NaN is in column 209 if that helps, but I will be using the same code for other files where the NaN is in other columns.
I'm new to Matlab so any help you guys can give would be massively appreciated, thank you in advance!

回答 (1 件)

KSSV
KSSV 2021 年 4 月 9 日
A = rand(5) ; % data for demo
A(:,2) = NaN ; % replace a column with NaN
for i = 1:5
if any(isnan(A(:,i)))
fprintf('%d column has NaN\n',i)
end
end
2 column has NaN

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by