How to solve floating point problem with datenum
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to find missing dates from a cell-array of dates. I have attached the cell-array with the question. My solution to the problem was as follows:
% create an array of time-steps with every datenum from starting to end date at required time-steps;
begin_date_time=datenum('2011-01-06 00:00:00','yyyy-mm-dd HH:MM:SS');
str_next_date=strcat('2011-01-06',32,'00:15:00');
next_date_time=datenum(str_next_date,'yyyy-mm-dd HH:MM:SS');
interval=next_date_time-begin_date_time;
end_date_time=datenum('2011-12-31 00:00:00','yyyy-mm-dd HH:MM:SS');
full_data_time_steps=begin_date_time:interval:end_date_time; % array of datenums containing all the time-steps between start and end dates with 15min interval
% convert the given cell arary to datenums
avail_time_steps=cellfun(@datenum,time_steps);
% then find missing datnums
[~,ind_full_data_tstep]=intersect(full_data_time_steps,...
avail_time_steps);
ind_missing=setdiff(1:length(full_data_time_steps),ind_full_data_tstep); % array of missing datenums
But it gives unexpected results because of floating-point errors. for example, it says that the date '2011-01-06 00:30:00' is missing from the cell-array 'time-steps', but it actually exists.
I don't know how to fix this issue. Any help is appreciated.
2 commentaires
dpb
le 6 Août 2018
I answered the previous Q? to show how to generate the datenum vector to match.
One can "cheat" and use ismembertol but that's more cumbersome and faking the right answer.
Réponses (1)
Peter Perkins
le 24 Août 2018
The real answer is, use datetime if at all possible, because this kind of floating point issue doesn't exist in datetime (at least not until you get to much higher resolutions).
But I gather that you are using R2014a, so that's not an option.
Voir également
Catégories
En savoir plus sur Time Series Objects dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!