1

I have two continuous ranges and wanna check whether they have any intersection or not in MATLAB. I know it can be achieved by a few if clauses but I wanna know if there is any function in MATLAB to do so.

Akram
  • 11
  • 2
  • 2
    please be more specific and post example data. – Robert Seifert Sep 10 '14 at 21:13
  • Maybe [something like this](http://www.mathworks.com/matlabcentral/fileexchange/31753-range-intersection) is what you're looking for? – Scott Solmer Sep 10 '14 at 21:14
  • for example x includes all values between 1 and 5 and y include all value between 2.5 and 7.2. is there any function to determine the intersection of these two ranges is not empty set? – Akram Sep 10 '14 at 21:16
  • With what level of precision? I mean values from 1 to 5 with an increment of 0.1 or 0.0001 for instance? – Benoit_11 Sep 10 '14 at 21:20
  • [some inspiration](http://stackoverflow.com/questions/20331097/synchronize-intersection-points-of-two-pairs-of-curves-with-fminsearch) – Robert Seifert Sep 10 '14 at 23:31

1 Answers1

0

Describe your ranges as a 1-by-2 arrays, and use this function:

function result = isRangesIntersect(range1, range2)

if range1(1) < range2(1)
    lowRange = range1;
    highRange = range2;
else
    lowRange = range2;
    highRange = range1;
end

result = lowRange(2) > highRange(1);

end

Let's say your first range is 1 to 5, and your second range is 2.5 to 7.2, then the call:

result = isRangesIntersect([1 5], [2.5 7.2])

Will return "true" (1). The call:

result = isRangesIntersect([2.5 7.2], [1 5])

Will also return "true".

Note that adjacent ranges will return "false". i.e. the call:

result = isRangesIntersect([1 5], [5 7])

Will return "false".

If you want adjacent ranges to return "true", then change the > operator on line 11 to the >= operator.

Shaked
  • 475
  • 1
  • 3
  • 12