Closest coordinate points between 2 data sets

4 次查看(过去 30 天)
Let's say: I have 2 dataset containning 3d points:
A=[1 1 1;1.5 1 1;2 1 1;2.5 1 1;3 1 1;1 2 1;3 2 1;1 3 1;1.5 3 1;2 3 1;2.5 3 1;3 3 1;1 1.5 1;1 2.5 1;3 1.5 1;3 2.5 1];
B=[2 1.1 1; 2 1.5 1;2 2 1; 2 2.5 1;2 2.9 1];%matrix contain 5 red points
How can we find the point belong to matrix B, that having the smallest distance to any point belong to matrix A?

采纳的回答

ha ha
ha ha 2019-2-24
clear;clc;
A=[1 1 1;1.5 1 1;2 1 1;2.5 1 1;3 1 1;1 2 1;3 2 1;1 3 1;1.5 3 1;2 3 1;2.5 3 1;3 3 1;1 1.5 1;1 2.5 1;3 1.5 1;3 2.5 1];
B=[2 1.1 1; 2 1.5 1;2 2 1; 2 2.5 1;2 2.9 1];
scatter3(A(:,1),A(:,2),A(:,3),51,'k.');
hold on;scatter3(B(:,1),B(:,2),B(:,3),91,'r.');
xlim([0 5]) ;ylim([0 5]);
%%
distances = pdist2(A(:, 1:2), B(:, 1:2));%calculate the distance between 2 datasets
minDistance = min(distances(:));%find the minimum value of distance
[rowOfA, rowOfB] = find(distances == minDistance);
B_coord_closest=B(rowOfB,:);%the point belong to B having the nearest distance to dataset A
hold on;scatter3(B_coord_closest(:,1),B_coord_closest(:,2),B_coord_closest(:,3),'*');
legend('dataset A','dataset B','the result point');
2.JPG
3.png

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by