我需要为2D平面生成N个随机坐标.给出任意两点之间的距离(距离的数量是N(N-1)/ 2).例如,假设我需要生成3个点,即A,B,C.我有一对它们之间的距离即distAB,distAC和distBC.
MATLAB中是否有内置函数可以做到这一点?基本上,我正在寻找与pdist()功能相反的东西.
我最初的想法是选择一个点(比如说A是原点).然后,我可以随机找到B和C在两个不同的圆上,半径distAB和distAC.但是B和C之间的距离可能不满足distBC,如果发生这种情况,我不确定如何继续.而且我认为如果N是一个很大的数字,这种方法会变得非常复杂.
阐述Ansaris答案,我做了以下内容.它假定提供一个有效的距离矩阵,计算基于在cmdscale 2D位置,做了随机的旋转(随机平移也可以加入),和可视化的结果:
%Distance matrix
D = [0 2 3; ...
2 0 4; ...
3 4 0];
%Generate point coordinates based on distance matrix
Y = cmdscale(D);
[nPoints dim] = size(Y);
%Add random rotation
randTheta = 2*pi*rand(1);
Rot = [cos(randTheta) -sin(randTheta); sin(randTheta) cos(randTheta) ];
Y = Y*Rot;
%Visualization
figure(1);clf;
plot(Y(:,1),Y(:,2),'.','markersize',20)
hold on;t=0:.01:2*pi;
for r = 1 : nPoints - 1
for c = r+1 : nPoints
plot(Y(r,1)+D(r,c)*sin(t),Y(r,2)+D(r,c)*cos(t));
plot(Y(c,1)+D(r,c)*sin(t),Y(c,2)+D(r,c)*cos(t));
end
end
Run Code Online (Sandbox Code Playgroud)
