MATLAB: Find the earliest common date and indicator of two date vectors.

compare date vectorsearliest date of several date vectors

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.

Best Answer

intersect will give you all the dates common to a and b. By default, it also sorts them, so the first of these will be the earliest one, so:
[commondates, whereina, whereinb] = intersect(a, b);
firstcommon = commondates(1);
whereina = whereina(1);
whereinb = whereinb(1);