Intersection of two matrices- intersect function not working as intended

조회 수: 5 (최근 30일)
Isaac John
Isaac John 2021년 2월 13일
댓글: Isaac John 2021년 2월 13일
I have two matrices, L1S (blue) and L2S (red), whose first 3 columns describe the x,y,z coordinates that I am interseted in.
As I require the intsersection of these two matrices, (only the first three columns) I have used XY11 = intesect(L1S(:,1:3),L2S(:,1:3),'rows'). But the plot of the intersection, XY11 is found to have skipped obvious points of intersection. (See fig below)
I cannot possibly think of a reason as to why this is happening. I have never faced this issue before. Please note that the points coordinates in both the matrices are exactly matching at all points.
Any leads to the solution is deepy appreciated.

채택된 답변

Jan
Jan 2021년 2월 13일
편집: Jan 2021년 2월 13일
With some guesses:
L1S_T = readtable('L1S.xlsx','ReadVariableNames', 0);
L1S = L1S_T.Variables;
L1S = L1S(:, 1:3);
L2S_T = readtable('L2S.xlsx', 'ReadVariableNames', 0);
L2S = L2S_T.Variables;
L2S = L2S(:, 1:3);
figure; axes('NextPlot', 'add');
plot(L1S(:, 1), L1S(:, 2), 'b.');
plot(L2S(:, 1), L2S(:, 2), 'r.');
L12 = intersect(L1S, L2S, 'rows');
plot(L12(:, 1), L12(:, 2), 'co');
This reproduces your result. This means, that the cyan values overlap, the others are different.
Now focus the point on the bottom left side of the assumed overlap:
P = [0.8163, 0.8082]
[~, index1] = min(vecnorm(L1S(:, 1:2) - c, 2, 2)) % 26
[~, index2] = min(vecnorm(L2S(:, 1:2) - c, 2, 2)) % 433
format long g
L1S(index1, :) % 0.816326530612245 0.808163265306123 0.06
L2S(index2, :) % 0.816326530612245 0.808163265306122 0.06
% ^
Do you see it? The difference is in the last significant digit. You find the difference in the Excel tables also: Select the 26th and 433th row in 2nd column. While the display in the table is limited to 10 digits, the display on the top shows the difference.
Solution:
Rounding to a specific number of decimals is not a perfect idea, because it will not recognize 0.4999999999 and 0.5 . Better use ismembertol with 'DataScale'.
L12i = ismembertol(L1S, L2S, 1e-8, 'ByRows', 1, 'DataScale', 1);
figure; axes('NextPlot', 'add');
plot(L1S(:, 1), L1S(:, 2), 'b.');
plot(L2S(:, 1), L2S(:, 2), 'r.');
plot(L1S(L12i, 1), L1S(L12i, 2), 'co');
  댓글 수: 5
Jan
Jan 2021년 2월 13일
A hint: While 10^4 is an expensive power operation, 1e4 is a cheap constant. round(x, 4) rounds to 4 decimal places. Rounding is dangerous:
x1 = 0.1234499999999999
x2 = 0.1234500000000000
round(x1, 4) == round(x2, 4) % FALSE !!!
Better:
abs(x1 - x2) < 1e4 % TRUE

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by