Closest coordinate points between 2 data sets

조회 수: 6 (최근 30일)
ha ha
ha ha 2019년 2월 24일
편집: ha ha 2019년 2월 24일
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에서 Assembly에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by