3
$\begingroup$

I have two individual Date lists as Date Objects:

List 1 = {1 Jan 1991 9 :00 , 3 Jan 1991 11:00 ..etc } 
List 2 ={ 7 Jan 1991 14:00, 9 Jan 1991 15:00 ..etc}

For any given date, example 5 Jan 1991 10:00 how can I check if this random date lies between corresponding elements in both lists i.e.,is (5 Jan 1991 10:00) is in between (1 Jan 1991 9:00) and (7 Jan 1991 14:00) . If it is true , then it should give the corresponding dates between which it falls.

I tried to use for loop. but its taking a lot of time as the data is huge.Is there any efficient way to code this logic?

$\endgroup$
  • $\begingroup$ Are the two lists sorted by date and time and every element is newer than the previous element? If so then perhaps a binary search to find the index of date in the first list that is most recent to your random date and do a single subscript to get the corresponding date from the second list? $\endgroup$ – Bill Mar 23 '16 at 2:30
  • $\begingroup$ Hi @Bill...Thanks for reply...but every element is sorted by date and time but some elements are same as the previous ones $\endgroup$ – no-one Mar 23 '16 at 2:33
3
$\begingroup$

Let's build up samples for your lists first:

SeedRandom[42];
at = AbsoluteTime;
l = Transpose@RandomInteger[{0, 360}, {50, 2}];
{l1, l2} = Map[DateString@DatePlus[{1991, 1, 1}, #] &, l, {2}];
myDate = at@"7 Jan 1991";

And compare:

p = MapThread[Less[#1, myDate, #2] || Less[#2, myDate, #1] &, Map[at, {l1, l2}, {2}]]
(*
{False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, False, False, \
False, False, False, False, False, False, False, False, True, False, \
False, False, False, False, False, False, False, False, False, False, \
False}
*)

The intervals that contain the desired date are:

Pick[Transpose@{l1, l2}, p] // TableForm

Mathematica graphics

And the non-conformant intervals are:

Pick[Transpose@{l1, l2}, Not /@ p] // TableForm

Mathematica graphics

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.