Finding the common range between two ranges

조회 수: 26 (최근 30일)
RAJAN PRASAD
RAJAN PRASAD 2019년 9월 3일
댓글: Adam Danz 2019년 9월 3일
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?
  댓글 수: 4
RAJAN PRASAD
RAJAN PRASAD 2019년 9월 3일
편집: RAJAN PRASAD 2019년 9월 3일
g5398.png
@Adam , Yes they are not common to the matrix. I have given a pictorial of what i need.
@kalyan I am not looking at the common entries but the common range. Thank you
Adam Danz
Adam Danz 2019년 9월 3일
편집: Adam Danz 2019년 9월 3일
The range is computed by
range(X1,2)
range(X2,2)
and again, there's not a single matching value. Is there supposed to be a matching value?
[addendum]
Based on the answer chosen, I now understand that you want to find the segments in X1 and X2 that overlap.

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
Bruno Luong 2019년 9월 3일
편집: Bruno Luong 2019년 9월 3일
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
  댓글 수: 2
RAJAN PRASAD
RAJAN PRASAD 2019년 9월 3일
Thank you
Adam Danz
Adam Danz 2019년 9월 3일
편집: Adam Danz 2019년 9월 3일
Yeah, good catch (comment removed); The series of comparisons was difficult to read, anyway. I added a new answer that offers an alternative to the FEX.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 9월 3일
This solution matches the outputs produced in Bruno's answer but is simpler and does not rely on external functions.
% Create every combination of [X1, X2] vectors
xCombos = [repelem(sort(X1,2),size(X2,1),1), repmat(sort(X2,2),size(X1,1),1)];
% Sort each row to get the sorted index
[~, sortIdx] = sort(xCombos,2);
% rows of [1 2 3 4] or [3 4 1 2] do not overlap. All others do.
overlapIdx = ~ismember(sortIdx,[1 2 3 4; 3 4 1 2],'rows');
% pull out the overlaps
I = [max(xCombos(overlapIdx,[1,3]),[],2), min(xCombos(overlapIdx,[2,4]),[],2)];
Method
xCombos is a matrix containing every combination of pairs between X1 and X2.
By sorting the rows of xCombos and looking at the sort index, we can identify which coordinates overlap. Non-overlapping coordinates will have a sorted index of [1 2 3 4] or [3 4 1 2] whereas overlapping combinations will have a different sort index.
After identiying which rows contain overlap, all you need to do is identify the inner values to create the bounds of the internal segment.
  댓글 수: 2
Bruno Luong
Bruno Luong 2019년 9월 3일
편집: Bruno Luong 2019년 9월 3일
The results won't match all the time. The FEX returns minimal number of intervals.
For example for
X1=[1 3; 4 6; 2 5]; X2=[0 4];
the FEX code
% https://fr.mathworks.com/matlabcentral/fileexchange/24254-interval-merging
[l,r] = RangeIntersection(X1(:,1),X1(:,2),X2(:,1),X2(:,2));
I = [l(:) r(:)];
returns
>> I =
1 4
Your code
% Create every combination of [X1, X2] vectors
xCombos = [repelem(sort(X1,2),size(X2,1),1), repmat(sort(X2,2),size(X1,1),1)];
% Sort each row to get the sorted index
[~, sortIdx] = sort(xCombos,2);
% rows of [1 2 3 4] or [3 4 1 2] do not overlap. All others do.
overlapIdx = ~ismember(sortIdx,[1 2 3 4; 3 4 1 2],'rows');
% pull out the overlaps
I = [max(xCombos(overlapIdx,[1,3]),[],2), min(xCombos(overlapIdx,[2,4]),[],2)];
returns
>> I
I =
1 3
4 4
2 4
Adam Danz
Adam Danz 2019년 9월 3일
Ah, I see. Nice function!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by