I have two sets of data as follows:
1-Data1 is a series of intervals. These intervals rarely overlap but they could.
2-Data2 consists of pairs of data where the first element of each pair could be or could not be a member of the intervals in Data1.
What I am looking for is a fast way to extract the data so that I get list which each element contains the interval from Data1 and all points from Data2 that their first elements lie within this interval.
As an example of what I am looking for is as follows:
n = 200;
intervals =
Sort[Partition[RandomReal /@ Table[{i, i + 5}, {i, 0, n, 6}], 2]];
v = RandomReal[n, Length[intervals] + RandomInteger[40]];
v2 = {#, RandomReal[{0.5, 0.8}]} & /@ v;
Plot[UnitStep[(x - #1) (#2 - x)] & @@@ intervals, {x, 0, 201},
Epilog -> {PointSize[0.005], Red, Point[v2]}, Filling -> Bottom,
GridLines -> None, Exclusions -> None, PlotPoints -> 100]
As you can see from this plot that some of the data points are within the intervals, but some are not.
I want to get pairs of data like following:
intervals2 =
Pick[intervals,
Or @@@ Transpose[
IntervalMemberQ[Interval /@ intervals, #] & /@ v2[[;; , 1]]]];
result = {#, Pick[v2, IntervalMemberQ[Interval@#, v2[[;; , 1]]]]} & /@
intervals2;
And now if i plot that i got:
Plot[UnitStep[(x - #1) (#2 - x)] & @@@ result[[;; , 1]], {x, 0, 201},
Epilog -> {{PointSize[0.005], Black,
Point[Flatten[result[[;; , 2]], 1]]}}, Filling -> Bottom,
GridLines -> None, PlotPoints -> 100]
This is really not an efficient way. My real Data1 has hundreds of thousands intervals and Data2 also contains hundreds of thousands pairs.
If i use the method mentioned above it will go forever.
I would appreciate it if someone can suggest a better and faster way.
Thank you