Closest coordinate points between 2 data sets

4 views (last 30 days)
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?

Accepted Answer

ha ha
ha ha on 24 Feb 2019
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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!