As far as I can see, the result of datenum
are simple numbers.
[~,idx1]=sort([T1+offset,T2]);
idx = find(idx1>length(T1));
idx = idx - (0:length(idx)-1);
If you leave out the offset
(or use 0
), this will give you for each element of T2
the index of the smallest element if T1
that is larger. To get to the one that is closest, add half the interval length in T1
to T1
(i.e. the datenum
equivalent of half a minute).
[edit]
If T1
does not consist of equidistant steps, one could try with a vector containing the middle of each interval in T1
instead.
T1m = [(T1(1:end-1) + T1(2:end))/2];
[~,idx1]=sort([T1m,T2]);
idx = find(idx1>length(T1m)) - (0:length(T2)-1);
[/edit]
How this works:
We first sort the vector of all time points, then ignore the actual result (replace ~
by a variable name, e.g. T
if you want to use it somehow). The second return value of sort
is the index of each entry of the sorted array in the original array. We want to know where the ones from T2
ended up, i.e. those that in the original, concatenated array [T1 T2]
have an index larger than the number of values in T1
, which is the idx
from the second line. Now these indices refer to the elements of the combined array, which means relative to T1
they are correct for the first element, off by one for the second (since the first element of T2
was thrown in before), off by two for the third (since two elements of T2
come before)..., which we correct in the third line.
You can combine the second and third line to idx = find(idx1>length(T1)) - (0:length(T2)-1);