Find the earliest common date and indicator of two date vectors.
Ältere Kommentare anzeigen
Hi,
I have got two date vectors: a=[28.02.2019;01.03.2019;04.03.2019;...] and b=[01.03.2019;04.03.2019;...]
How to find the earliest common date (=01.03.2019) and its vector index indicator (for date vector a is 2 and b is 1).
Thank you in advance for any suggestions.
Akzeptierte Antwort
Weitere Antworten (1)
This answer assumes you have your dates in a datetime format. If you don't it should be relatively easy to convert to it. If your release is too old to support it, please comment with what release you're using.
a=datetime([2019 2019 2019],[2 3 3],[28 1 4]);
b=datetime([2019 2019 2019],[3 3 5],[1 4 1]);
[common_dates,ind_in_a]=ismember(a,b);
common_dates=a(common_dates);
[first,a_min_ind]=min(common_dates);
a_ind=find(ind_in_a==a_min_ind);
b_ind=find(b==first);
4 Kommentare
BdS
am 17 Mai 2019
Rik
am 17 Mai 2019
For what cases doesn't this work? This code does not assume equal length of the vector. It works for most shapes that have no zero-length dimension.
Rik
am 17 Mai 2019
I edited my answer. It works now, but it has become messy, so I would suggest you keep using Guillaume's solution.
Kategorien
Mehr zu Time Series Objects finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!