[英]How to connect a 3D points with a distance threshold Matlab
我有一個3D點矢量讓我們說A如下所示,
A=[
-0.240265581092000 0.0500598627544876 1.20715641293013
-0.344503191645519 0.390376667574812 1.15887540716612
-0.0931248606994074 0.267137193112796 1.24244644549763
-0.183530493218807 0.384249186312578 1.14512014134276
-0.0201358671977785 0.404732019283683 1.21816745283019
-0.242108038906952 0.229873488902244 1.24229940627651
-0.391349107031230 0.262170158259873 1.23856838565023
]
我想要做的是將3D點連接到距離小於特定閾值T
。 我想得到一個需要連接的點對列表。 如,
[
( -0.240265581092000 0.0500598627544876 1.20715641293013), (-0.344503191645519 0.390376667574812 1.15887540716612);
(-0.0931248606994074 0.267137193112796 1.24244644549763),(-0.183530493218807 0.384249186312578 1.14512014134276),.....
]
如圖所示,我將有一對需要連接的點對向量。 所以,如果有人可以請在Matlab中建議如何做到這一點。
以下示例演示了如何完成此操作。
%# Build an example matrix
A = [1 2 3; 0 0 0; 3 1 3; 2 0 2; 0 1 0];
Threshold = 3;
%# Calculate distance between all points
D = pdist2(A, A);
%# Discard any points with distance greater than threshold
D(D > Threshold) = nan;
如果你想提取所有通過小於(或等於) Threshold
的距離鏈接的觀察對的索引,以及相應的距離(你的問題沒有指明你想要輸出的形式,所以我我基本上在猜這里),然后使用以下內容:
%# Obtain a list of linear indices of observations less than or equal to TH
I1 = find(D <= Threshold);
%#Extract the actual distances, as well as the corresponding observation indices from A
[Obs1Index, Obs2Index] = ind2sub(size(D), I1);
DList = [Obs1Index, Obs2Index, D(I1)];
注意, pdist2
默認使用歐幾里德距離,但還有其他選項 - 請參閱此處的文檔。
更新:根據OP的注釋,下面的代碼將輸出表示為K*6
矩陣,其中K
是小於閾值的距離度量數,每行的前三列是第一個數據點( 3維)和每行的后三列是連接的數據點。
DList2 = [A(Obs1Index, :), A(Obs2Index, :)];
第二更新:我沒有對這個答案中的距離測量做出任何假設。 也就是說,我故意使用pdist2
以防你的距離測量不對稱。 但是,如果您使用對稱距離度量,那么您可以通過使用pdist
來加快運行時間,盡管我的索引代碼需要相應地進行調整。
Plot3
和pdist2
可用於實現您想要的效果。
D=pdist2(A,A);
T=0.2;
for i=1:7
for j=i+1:7
if D(i,j)<T & D(i,j)~=0
i
j
plot3(A([i j],1),A([i j],2),A([i j],3));
hold on;
fprintf('line is plotted\n');
pause;
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.