Reputation: 73
I need to create a bin list in which I test a datetime list against another datetime list of different sizes. Basically, I have a bigger list (tb_date - 81504 samples) and want to know where some datetimes (smaller list - (useful_date - 42443 samples)) are located at it - returning zero if the date is not present in the smaller list, and 1 if it is present - creating a new list in the size of the tb_date composed by 0 or 1s (item 1 is not present, 0, item 2 is present, 1, and so on.. I have made:
[m,n] = size(tb_date);
[i,j] = size(useful_date);
tb_useful_ind = []
for k = 1:n
for i = 1:j
if tb_date(k) == useful_date(j)
tb_useful_ind(k) = 1;
else
tb_useful_ind(k) = 0;
end
end
end
and then, to return the indexes in the tb_date:
date_indexes = find(tb_useful_ind);
But it is impossible to use like this - it is returning me wrong values and also taking ages.. (9h running and only gave me 5k elements).. How would you recommend me doing it, please?
Thank you very much.
UPDATE: I tried to use the intersect:
>> intersect(tb_date,datetime_day); useful_date2 = ans;
>> B = tb_date == useful_date2;
Data inputs must be the same size, or any of them can be a scalar.
Also tried:
ind=find(tb_date==useful_date);
Data inputs must be the same size, or any of them can be a scalar.
I need to obtain a list of the same size of tb_date (81504), in which each element of tb_date is checked if it is in the useful_date and returns me 0 if the element isn't, and 1 if the element is. For example:
tb-date = '01-Jan-2016 00:00:00' '01-Jan-2016 00:15:00' '01-Jan-2016 00:30:00' '01-Jan-2016 00:45:00' '01-Jan-2016 01:00:00' '01-Jan-2016 01:15:00' '01-Jan-2016 01:30:00' '01-Jan-2016 01:45:00' '01-Jan-2016 02:00:00' '01-Jan-2016 02:15:00' '01-Jan-2016 02:30:00' '01-Jan-2016 02:45:00' '01-Jan-2016 03:00:00'
useful_date = '01-Jan-2016 01:00:00' '01-Jan-2016 01:15:00' '01-Jan-2016 01:30:00'
The output I need, roughly speaking, is:
date_indexes = (0 0 0 0 1 1 1 0 0 0 0 0 0)
Upvotes: 0
Views: 77
Reputation: 2438
You can do a set intersect to find the common values, this will not include any repeat values. For example, I will use randomly generate datetime value:
>> timepoint = datetime("now")
timepoint =
datetime
21-Aug-2020 00:16:21
>> dates_1 = reshape(randi(30, 10) + timepoint, [1 100]); dates_2 = reshape(randi(30, 10) + timepoint, [1 100]);
>> intersect(dates_1, dates_2)
ans =
29×1 datetime array
22-Aug-2020 00:16:21
23-Aug-2020 00:16:21
24-Aug-2020 00:16:21
25-Aug-2020 00:16:21
26-Aug-2020 00:16:21
27-Aug-2020 00:16:21
28-Aug-2020 00:16:21
...
Edit:
If you wanted to their locations, you could use find
, loop through each element in the usefuldates datetime array, and run find(tb_dates == element)
. Alternatively, intersect optionally returns two additional arguments, [C,ia,ib] = intersect(___)
, which are the indices of the common values, in both arrays.
Upvotes: 3