Intersection of two matrices- intersect function not working as intended

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.

 Accepted Answer

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 Comments

Thank you very much. I didnt expect the tolerance between L1S and L2S as they come from the same parent data set. Really appreciate your time and effort.
You are welcome. You have observed this difference already by your own, but as far as I understand you did not trust Matlab's results. All I did was believing that the output of your code is correct already.
In all other cases, the 'intersect' was working as intended, but with here, I was caught off guard with the difference. How the tolerance crept into the data sets L1S and L2S, I will have to figure that out. As a desperate measure, I would have tried something like
L1S = round(L1S*10^4)/10^4 and L2S = round(L2S*10^4)/10^4.
Thanks for pointing out the 'ismembertol'. I would have never got that far.
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

Sign in to comment.

More Answers (0)

Products

Release

R2016a

Asked:

on 13 Feb 2021

Commented:

on 13 Feb 2021

Community Treasure Hunt

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

Start Hunting!