Mapping a numeric array to a second numeric array

I am writing because I was wondering if you guys have a suggestion for the following problem with displaying an array in MATLAB

I have a time array, a year in one minute increments (T1), and another time array (T2) that is non-uniformly distributed and not (necessarily) overlapping with T1. An example would be:

T1 = [1-Jan-2011 00:01:23, 1-Jan-2011 00:02:23.... end of year 2011]
T2 = [1-Jan-2011 00:04:12, 1-Jan-2011 03:014:54, ....]

      

T1 and T2 are actually in format datenum

, but I wanted to give a clear example here.

The length of two is not the same, ( length(T1) ~ 5*length(T2)

), but I know that no two elements of T2 are in the same interval in T1. I mean that a T2 item will always be uniquely identified as one of the T1s.

I want to do (efficient = fast) a mapping from T2 to T1, so that I have a set of idx indices so that T1(idx(n))

is the closest time to T2(n)

. I already have a routine, but it's a bit slow.

Suggestions?

Many thanks! Riccardo

+3


source to share


1 answer


As far as I can see, the result datenum

is prime numbers.

[~,idx1]=sort([T1+offset,T2]);
idx = find(idx1>length(T1));
idx = idx - (0:length(idx)-1);

      

If you leave offset

(or use 0

) it will give you, for each element, the T2

index of the smallest element, if T1

greater. To get to the closest one, add half the length of the interval in T1

to T1

(i.e. the equivalent datenum

of half a minute).

[edit] If it T1

does not consist of equidistant steps, you can try to use a vector containing the midpoint of each interval in T1

.

T1m = [(T1(1:end-1) + T1(2:end))/2];
[~,idx1]=sort([T1m,T2]);
idx = find(idx1>length(T1m)) - (0:length(T2)-1);

      



[/ edit]

How it works:

First we sort the vector of all time points, then ignore the actual result (replace ~

with the variable name, for example T

if you want to use it in some way). The second return value sort

is the index of each sorted array entry in the original array. We want to know where those of the ends ended T2

, that is, those in the original concatenated array [T1 T2]

have an index greater than the number of values ​​in T1

, which is idx

from the second row.Now these indices refer to the elements of the concatenated array, which means that with respect to T1

they are true for the first item, with one for the second (since the first item from T2

was inserted earlier), off. two for the third (since two elementsT2

come earlier) ... which we correct in the third line.

You can combine the second and third lines with idx = find(idx1>length(T1)) - (0:length(T2)-1);

+1


source







All Articles