Finding the common range between two ranges

16 visualizzazioni (ultimi 30 giorni)
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 Commenti
RAJAN PRASAD
RAJAN PRASAD il 3 Set 2019
Modificato: RAJAN PRASAD il 3 Set 2019
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 il 3 Set 2019
Modificato: Adam Danz il 3 Set 2019
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.

Accedi per commentare.

Risposta accettata

Bruno Luong
Bruno Luong il 3 Set 2019
Modificato: Bruno Luong il 3 Set 2019
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 Commenti
Adam Danz
Adam Danz il 3 Set 2019
Modificato: Adam Danz il 3 Set 2019
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.

Accedi per commentare.

Più risposte (1)

Adam Danz
Adam Danz il 3 Set 2019
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 Commenti
Bruno Luong
Bruno Luong il 3 Set 2019
Modificato: Bruno Luong il 3 Set 2019
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 il 3 Set 2019
Ah, I see. Nice function!

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by