Closest coordinate points between 2 data sets

5 ビュー (過去 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 Center および File ExchangeAssembly についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by