how to solve if interpolated data also contains NaN values
Mostrar comentarios más antiguos
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 comentarios
Dyuman Joshi
el 13 de Mzo. de 2024
Also, use a tolerance to compare floating point values. You could also replace the loop with logical indexing.
In any case, please share your data so that we can reproduce the results you obtained. Use the paperclip button to attach.
Ismita
el 13 de Mzo. de 2024
Dyuman Joshi
el 13 de Mzo. de 2024
Please save the data in a file and then upload the file.
A general note - The data displayed is not the same as data stored.
Ismita
el 13 de Mzo. de 2024
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!