MATLAB: Finding the common range between two ranges

MATLABsort

I have two matrix such as
X1=[0.316227766016838 0.346410161513775
0.509901951359279 0.529150262212918
1.63401346383682 1.63707055437449
2.28035085019828 2.37276210354093
3.24961536185438 4.78330429724056
4.96689037527506 5.00199960015992
5.06655701635736 6.06959636219741
6.07782855960910 9.43292107461946
9.61145150328503 10 ]
X2=[0.223606797749979 0.223606797749979
0.447213595499958 0.458257569495584
2.11423745118660 2.14242852856286
2.22485954612870 2.27815714997890
3.24653661614959 3.24807635378234
3.28329103187640 4.72652091923859
4.91121166312347 4.94064773081425
5.00299910053960 6.06959636219741
6.07865116617165 9.61093127641645
9.78110423214066 10]
I need to know the common ranges between X1 and X2 if it exist. Dimensions of X1 and X2 are not same
For example (3.28329103187640 4.72652091923859) range is common to both X1 and X2
Similarlyand 5.06655701635736 6.06959636219741 and so-on.
Can anybody help me how to do this in matlab?

Best Answer

You can use this RangeIntersection function on FEX
% Test data
X1=[0.316227766016838 0.346410161513775
0.509901951359279 0.529150262212918
1.63401346383682 1.63707055437449
2.28035085019828 2.37276210354093
3.24961536185438 4.78330429724056
4.96689037527506 5.00199960015992
5.06655701635736 6.06959636219741
6.07782855960910 9.43292107461946
9.61145150328503 10 ]
X2=[0.223606797749979 0.223606797749979
0.447213595499958 0.458257569495584
2.11423745118660 2.14242852856286
2.22485954612870 2.27815714997890
3.24653661614959 3.24807635378234
3.28329103187640 4.72652091923859
4.91121166312347 4.94064773081425
5.00299910053960 6.06959636219741
6.07865116617165 9.61093127641645
9.78110423214066 10]
% https://fr.mathworks.com/matlabcentral/fileexchange/24254-interval-merging
[l,r] = RangeIntersection(X1(:,1),X1(:,2),X2(:,1),X2(:,2));
I = [l(:) r(:)]
It returns
I =
3.2833 4.7265
5.0666 6.0696
6.0787 9.4329
9.7811 10.0000