Effacer les filtres
Effacer les filtres

How to find the closest rows (positions/entities) in two mby3 matrixes?

1 vue (au cours des 30 derniers jours)
Alireza Babaei
Alireza Babaei le 28 Avr 2023
Commenté : dpb le 28 Avr 2023
Hi all,
I have 2 matrices both with 3 columns as the x, y, and z positions. My question is how can I find the row numbers which are closest to each other? I was thinking of: min(abs(Matrix_A - matrix_B)
but this does not give me the row number. Any ideas on how to find the row number?
for simplicity consider row number 5 from matrix_A is to be matched with the closest row on matrix_B.
Thanks!
A = rand(10 , 3) % find the closest row in B to the 5th row in A
B = rand(10 , 3)
closest = min(abs(A(5 , :) - B))

Réponses (1)

Image Analyst
Image Analyst le 28 Avr 2023
Try this. Adapt as needed:
% Create sample data.
xyz1 = rand(4, 3);
xyz2 = rand(4, 3) + 1.5;
plot3(xyz1(:, 1), xyz1(:, 2), xyz1(:, 3), 'b.', 'MarkerSize',20);
grid on;
hold on;
plot3(xyz2(:, 1), xyz2(:, 2), xyz2(:, 3), 'r.', 'MarkerSize',20);
% Find distance between every point and every other point.
distances = pdist2(xyz1, xyz2)
distances = 4×4
2.9060 2.2501 2.3816 2.9554 3.5615 2.8735 3.0348 3.5420 2.9738 2.3935 2.5318 2.9725 3.0813 2.3573 2.5414 2.9943
% Find minimum distance
minDistance = min(distances(:))
minDistance = 2.2501
% Find index
[row1, row2] = find(distances == minDistance)
row1 = 1
row2 = 2
% Draw a line between the two closest (x,y,z) points
p1 = xyz1(row1, :)
p1 = 1×3
0.8522 0.0734 0.8325
p2 = xyz2(row2, :)
p2 = 1×3
1.6421 1.6117 2.2722
plot3([p1(1), p2(1)], [p1(2), p2(2)], [p1(3), p2(3)], 'g-', 'LineWidth', 4);
  1 commentaire
dpb
dpb le 28 Avr 2023
As per usual, great example by @Image Analyst.
Only recast to use the optional alternate input parameter of pdist2 to return N 'smallest' or 'largest' values; and an index array into the output distances of the locations of the other array. That's a little confusing in that it doesn't return the one overall global minimum as one might think, but studying the outuput a little will clarify what it actually does return. Overall, there's not much real difference other than the potential size of the output returned; I don't know that it can save any compute time; it might even be something more internally because it's also got to do the calculation to find the minimal ones...and that just might be more than simply computing them all and then doing the global search. I've not tried timing it on large problems to explore...
% Create sample data.
xyz1 = rand(4, 3);
xyz2 = rand(4, 3) + 1.5;
plot3(xyz1(:, 1), xyz1(:, 2), xyz1(:, 3), 'b.', 'MarkerSize',20);
grid on;
hold on;
plot3(xyz2(:, 1), xyz2(:, 2), xyz2(:, 3), 'r.', 'MarkerSize',20);
% Find distance between every point and every other point.
[distances,index] = pdist2(xyz1, xyz2,'euclidean','smallest',1)
distances = 1×4
1.2651 1.6353 1.3652 2.4661
index = 1×4
3 3 3 3
% Find minimum distance
% Draw a line between the two closest (x,y,z) points
[~,row2]=min(distances);
row1=index(row2);
p1 = xyz1(row1, :)
p1 = 1×3
0.9864 0.7630 0.9516
p2 = xyz2(row2, :)
p2 = 1×3
1.6765 1.5761 1.6321
plot3([p1(1), p2(1)], [p1(2), p2(2)], [p1(3), p2(3)], 'g-', 'LineWidth', 4);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by