Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
BdS
BdS am 17 Mai 2019
Kommentiert: Rik am 17 Mai 2019
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.
  2 Kommentare
Guillaume
Guillaume am 17 Mai 2019
"I have got two date vectors"
Please use valid matlab syntax for your examples.
In matlab a date vector is a 1x3 or 1x6 vector containing only 1 date.
a = [28.02.2019; 01.03.2019; 04.03.2019];
is not valid matlab syntax. So what do you have?
BdS
BdS am 17 Mai 2019
Thank you for your code suggestion.
I have got a 55x1 datetime date vector which is vector A and a 56x1 datetime date vector which is vector B.
The date format is 06-Mar-2019
----
It does not have to be that the time vector A is always smaller than Vector B. It can change. It can also be that the first date of time vector A begins later than in date vector B. It must be flexible.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 17 Mai 2019
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);

Weitere Antworten (1)

Rik
Rik am 17 Mai 2019
Bearbeitet: Rik am 17 Mai 2019
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
BdS am 17 Mai 2019
Bearbeitet: Guillaume am 17 Mai 2019
With your code I get for a the indicator 1 and for b 1 as well.
I should actually get for a indicator 2 and b indicator 1. Both indicates 01-Mar-2019 in vector a and b.
The code should also work in cases when b begins with an earlier date.
Rik
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Time Series Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by