how to solve if interpolated data also contains NaN values
2 次查看(过去 30 天)
显示 更早的评论
I was trying to interpolate 'lat' and 'long' columns but even after interpolation first 4 data are still showing as NaN. For other data, interpolation is ok. I request for help /suggestion for the first 4 data. Thanks!
% Initialize lat_clean and long_clean with NaN values
lat_clean = NaN(size(lat));
long_clean = NaN(size(long));
% Update lat_clean and long_clean if the corresponding values in lat and long are not 9999.9
for i = 1:length(lat)
if (lat(i) ~= 9999.9) && (long(i) ~= 9999.9)
lat_clean(i) = lat(i);
long_clean(i) = long(i);
end
end
% Linear interpolation of NaN data
t_lat = 1:numel(lat_clean);
nan_lat = isnan(lat_clean);
lat_clean(nan_lat) = interp1(t_lat(~nan_lat), lat_clean(~nan_lat), t_lat(nan_lat));
t_long = 1:numel(long_clean);
nan_long = isnan(long_clean);
long_clean(nan_long) = interp1(t_long(~nan_long), long_clean(~nan_long), t_long(nan_long));
% Concatenate data to form new matrix
new_data = [time_numeric, density_dir, speed_dir, temperature_dir, distance_AU, lat_clean, long_clean];
% Write the new data to a text file
NHVOY2_Data = 'NH_VOY2_Data.txt'; % Replace with the desired output file path
dlmwrite(NHVOY2_Data, new_data, 'delimiter', '\t', 'precision', '%.6f');
result showing as
lat_clean =
NaN
NaN
NaN
NaN
-1.4000
-1.2000
-0.9000
-1.0000
-0.3000
采纳的回答
Chunru
2024-3-13
a = readmatrix("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1641181/V2_small.txt");
a(a>9999.8) = nan
b = fillmissing(a, 'linear')
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!