Can I use ismembertol to find x,y coordinates that are close to each other?

15 次查看(过去 30 天)
I want to compare coordinates from one array to coordinates from another. Can this work since I cannot directly put ordered pairs into a matrix in MATLAB?

回答(2 个)

Jon
Jon 2023-7-19
编辑:Jon 2023-7-19
If I am understanding what you are trying to do correctly I think this would do what you ask
% Define some example coordinates
A = [1.3 2.8
2.5 3.9
6.2 8.7
5.4,4.3];
B = [2.49 3.88
5.39,4.31];
% Define tolerance
tol = 0.02;
% Find points in A that match B
Amatch = A(ismembertol(A,B,tol,'ByRows',true),:)
Amatch = 2×2
2.5000 3.9000 5.4000 4.3000
  4 个评论
Jon
Jon 2023-7-19
As @Star Strider suggests, rather than putting tolerances separately on the x and y coordinates, the Euclidean distance from the points in B to the points in A is a good criteria for judging whether they approximately match.
If you have the Statistical and Machine Learning toolbox, then you can use pdist2 for this as described by @Star Strider.
If you don't have the Statistical and Machine Learning toolbox, you could compute the distances and use them as follows
% Define some example coordinate
A = [1.3 2.8
2.5 3.9
6.2 8.7
5.4,4.3];
B = [2.49 3.88
5.39,4.31];
% Define tolerance for matching points
tol = 0.05; % adjust for your needs
% Find distance from each point in B to each point in A
% in a distance matrix, D, with D(i,j) giving distance from ith point in A
% to jth point in B
% Represent 2d points as complex values for easy distance manipulation as
% vectors instead of m by 2 matrices
Ac = A*[1;1i];
Bc = B*[1;1i];
D = abs(Ac - Bc.'); % matrix whose i,j entry give d
% Find matching rows in A corresponding to each row in B based on distance
% tolerance
idx = find(D<=tol); % return linear indices into distance matrix
[r,~] = ind2sub(size(D),idx); % find row in A which matches
% List the matching points
Amatch = A(r,:)
Amatch = 2×2
2.5000 3.9000 5.4000 4.3000
% Or, you could just find the matches based on the closest value without a
% specific tolerance
[~,r] = min(D);
Amatch = A(r,:)
Amatch = 2×2
2.5000 3.9000 5.4000 4.3000

请先登录,再进行评论。


Star Strider
Star Strider 2023-7-19
The best approach to find (x,y) pairs that are close to each other is probably to use pdist2. You can then compare the distances. The ismember function can then be useful in finding the closest pairs.
Example —
A = randn(10,2)
A = 10×2
-0.9422 0.0717 2.3691 1.7775 -0.8306 -0.4153 -2.1050 0.2304 0.3308 1.3710 -0.7083 -1.1922 -0.3769 0.3738 -0.2065 -1.2760 1.6947 -0.2010 0.9019 -0.7153
B = randn(9,2)
B = 9×2
0.0959 2.3267 -0.3266 1.3585 -1.1101 -0.2051 1.8024 -0.4148 -0.9464 1.4494 -0.8747 2.5306 -0.8180 -1.0263 -2.6085 -0.1066 1.2114 -0.5300
[D,I] = pdist2(A,B, 'euclidean','smallest',size(A,1))
D = 10×9
0.9841 0.6575 0.3237 0.2394 1.2171 1.6726 0.1989 0.6059 0.3607 2.0093 0.9860 0.3497 0.9493 1.2796 2.2135 0.6111 1.6758 0.5847 2.3386 1.4265 0.9342 2.1858 1.3777 2.4598 0.6605 1.8045 1.6022 2.4825 1.8440 1.0657 2.2643 1.6817 2.6085 1.1050 2.1884 1.8274 2.8942 2.1060 1.0860 2.3140 1.8683 2.9462 1.4679 2.2827 2.0307 2.9908 2.4103 1.4012 2.3176 2.6523 3.3301 1.7478 2.6715 2.0452 3.0394 2.5529 2.0757 2.6283 2.8241 3.7002 1.7988 3.2898 2.0950 3.1469 2.5791 2.1355 2.6330 2.8464 3.7265 2.6447 3.5628 2.2360 3.6095 2.6373 2.8048 2.7874 3.1144 3.7501 2.6584 4.3042 2.5816 3.6153 2.7281 4.0045 3.9603 3.3317 3.8648 4.2449 5.3222 3.4024
I = 10×9
5 5 1 9 7 5 6 4 10 7 7 3 10 5 7 3 1 9 2 1 7 8 1 1 8 3 8 1 3 6 2 4 4 1 6 7 3 4 4 5 3 3 7 7 6 9 10 8 7 6 2 10 8 3 4 9 10 6 8 10 4 5 5 10 6 5 3 10 6 9 10 1 6 8 9 1 9 9 5 9 2 8 2 2 4 2 8 2 2 4
Ds = sort(D(:),'ascend');
[~,idx] = ismember(D,Ds(1:10)); % Return Informaton For The Closest 10 Pairs
[r,c] = arrayfun(@(x)find(idx == x), 1:10); % Return Row & Col Indices
DistIdx = table(Ds(1:10),r(:),c(:), 'VariableNames',{'Distance','A Row','B Row'}) % Results Table
DistIdx = 10×3 table
Distance A Row B Row ________ _____ _____ 0.19885 1 7 0.23937 1 4 0.32372 1 3 0.34974 2 3 0.36068 1 9 0.58469 2 9 0.60585 1 8 0.61113 2 7 0.65747 1 2 0.66046 3 7
Make appropriate changes to get the desired result.
.

类别

Help CenterFile Exchange 中查找有关 Get Started with Statistics and Machine Learning Toolbox 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by