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');

