Effacer les filtres
Effacer les filtres

how to find nearest date corresponding value ?

18 vues (au cours des 30 derniers jours)
pruth
pruth le 17 Oct 2019
Commenté : pruth le 17 Oct 2019
Hi, i am really stuck here and can not move ahead !
i have written this code to match the date which works fine as expected.
load('date.mat ');
load('AOD.mat ');
N = AOD_440(:,1);
V = A(:,1);
B = repmat(N,[1 length(V)])
[minValue,closestIndex] = min(abs(B-V'));
closestValue = N(closestIndex);
but here AOD_440 has second column which has some values. so when above code is finding its closest date it should put the corresponding value in second column !
how can we just update the above code ?
or do I need to write different method code ?
i hope you understand the question !
  2 commentaires
Sebastian Bomberg
Sebastian Bomberg le 17 Oct 2019
Have you considered working with timetables? That way you would not have to deal with repmat to find the closest dates.
I presume A and AOD_440(:,1) are proper dates encoded in a datenum fashion.
sampleTime = datetime(AOD_440(:,1),'ConvertFrom','datenum');
queryTime = datetime(A,'ConvertFrom','datenum');
TT = timetable(AOD_440(:,2),'RowTimes',sampleTime)
TT2 = retime(TT,queryTime,'nearest')
If you wanted the timestamp of the samples nearest to the queryTimes you can add sampleTime as another variable to the timetable.
pruth
pruth le 17 Oct 2019
Thats works fine too !! thanks !

Connectez-vous pour commenter.

Réponse acceptée

Jos (10584)
Jos (10584) le 17 Oct 2019
Does this return what you want?
R = AOD_440(closestIndex, [1 2]) % select first (date?) and second (values?) columns
  1 commentaire
pruth
pruth le 17 Oct 2019
seriously ???? it was that easy ??

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by