我有一个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中建议如何做到这一点。
答案 0 :(得分:2)
以下示例演示了如何完成此操作。
%# 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
默认情况下使用欧几里德距离,但还有其他选项 - 请参阅文档here。
更新:根据OP的评论,以下代码将输出表示为K*6
矩阵,其中K
是距离度量小于阈值的数量值,每行的前三列是第一个数据点(3个维度),每行的后三个列是连接的数据点。
DList2 = [A(Obs1Index, :), A(Obs2Index, :)];
第二次更新:我没有对此答案中的距离测量做出任何假设。也就是说,如果您的距离测量不对称,我会故意使用pdist2
。但是,如果您使用的是对称距离度量,那么您可以使用pdist
来改善运行时间,尽管我的索引代码需要相应调整。
答案 1 :(得分:1)
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