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
댓글 수: 5
채택된 답변
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!