6

我有一个大约 144 点的区域。我想要实现的是测量一个点与所有其他点的距离并将其存储在一个数组中。我想为所有点都这样做。如果可能的话,我想以不重复的方式存储这些数据。而且我应该能够进行查询,例如 - 所有点之间的所有距离而不重复,点 no56 的所有距离之和等。

我有一个 3*144 数组,其中两列存储点的坐标。

4

3 回答 3

5

A possible solution (I am not really clear with what you mean by no repetition, though):

 X are your points with coordinates x = X(:,1), y = X(:,2)


 dist = sqrt(bsxfun(@minus,X(:,1),X(:,1)').^2 + bsxfun(@minus,X(:,2),X(:,2)').^2)

so

dist(i,j) is the euclidean distance between i and j

of course the matrix is symmetric. You can easily reduce the complexity involved.

于 2013-01-23T15:52:44.993 回答
2

假设您的数组是A,其中每列存储一个点的坐标。要获得所有点对的组合(不重复),请使用nchoosek

pairs = nchoosek(1:size(A, 2), 2)

然后像这样计算欧几里得距离

dist = sqrt(sum((A(:, pairs(:, 1)) - A(:, pairs(:, 2))) .^ 2, 1))

如果您安装了Statistics Toolbox,则可以使用它pdist(A)来获得相同的效果。

于 2013-01-23T16:02:16.860 回答
1

如果您有统计工具箱,并且您的所有数据都在数组 X 中,那么

D = pdist(X)

给出 X 中所有点之间的所有成对距离.

于 2014-05-05T18:35:49.523 回答