[英]Matlab calculating nearest neighbour distance for all (u, v) vectors in an array
I am trying to calculate the distance between nearest neighbours within a nx2 matrix like the one shown below 我正在尝试计算nx2矩阵内的最近邻居之间的距离,如下所示
point_coordinates =
11.4179 103.1400
16.7710 10.6691
16.6068 119.7024
25.1379 74.3382
30.3651 23.2635
31.7231 105.9109
31.8653 36.9388
%for loop going from the top of the vector column to the bottom
for counter = 1:size(point_coordinates,1)
%current point defined selected
current_point = point_coordinates(counter,:);
%math to calculate distance between the current point and all the points
distance_search= point_coordinates-repmat(current_point,[size(point_coordinates,1) 1]);
dist_from_current_point = sqrt(distance_search(:,1).^2+distance_search(:,2).^2);
%line to omit self subtraction that gives zero
dist_from_current_point (dist_from_current_point <= 0)=[];
%gives the shortest distance calculated for a certain vector and current_point
nearest_dist=min(dist_from_current_point);
end
%final line to plot the u,v vectors and the corresponding nearest neighbour
%distances
matnndist = [point_coordinates nearest_dist]
I am not sure how to structure the 'for' loop/nearest_neighbour line to be able to get the nearest neighbour distance for each u,v vector. 我不确定如何构造“ for”循环/ nearest_neighbour线以获取每个u,v向量的最近邻居距离。
I would like to have, for example ; 例如,我想拥有; for the first vector you could have the coordinates and the corresponding shortest distance, for the second vector another its shortest distance, and this goes on till n
对于第一个向量,您可以具有坐标和相应的最短距离,对于第二个向量,则可以具有其最短距离,一直到n
Hope someone can help. 希望有人能帮忙。
Thanks 谢谢
I understand you want to obtain the minimum distance between different points . 我了解您想获得不同点之间的最小距离 。
You can compute the distance for each pair of points with bsxfun
; 您可以使用
bsxfun
计算每对点的距离; remove self-distances; 消除自我距离; minimize.
最小化。 It's more computationally efficient to work with squared distances, and take the square root only at the end.
使用平方距离,并且仅在最后求平方根,计算效率更高。
n = size(point_coordinates,1);
dist = bsxfun(@minus, point_coordinates(:,1), point_coordinates(:,1).').^2 + ...
bsxfun(@minus, point_coordinates(:,2), point_coordinates(:,2).').^2;
dist(1:n+1:end) = inf; %// remove self-distances
min_dist = sqrt(min(dist(:)));
Alternatively, you could use pdist
. 另外,您可以使用
pdist
。 This avoids computing each distance twice, and also avoids self-distances: 这避免了两次计算每个距离,也避免了自距离:
dist = pdist(point_coordinates);
min_dist = min(dist(:));
If I can suggest a built-in function, use knnsearch
from the statistics toolbox. 如果我可以建议内置函数,请使用统计信息工具箱中的
knnsearch
。 What you are essentially doing is a K-Nearest Neighbour (KNN) algorithm, but you are ignoring self-distances. 本质上,您正在做的是K最近邻(KNN)算法,但是您忽略了自身距离。 The way you would call
knnsearch
is in the following way: 调用
knnsearch
的方式如下:
[idx,d] = knnsearch(X, Y, 'k', k);
In simple terms, the KNN algorithm returns the k
closest points to your data set given a query point. 简单来说,KNN算法会根据查询点将
k
最接近的点返回到您的数据集。 Usually, the Euclidean distance is the distance metric that is used. 通常,欧几里得距离是所使用的距离度量。 For MATLAB's
knnsearch
, X
is a 2D array that consists of your dataset where each row is an observation and each column is a variable. 对于MATLAB的
knnsearch
, X
是一个2D数组,由您的数据集组成,其中每一行都是观察值,每一列都是变量。 Y
would be the query points. Y
将是查询点。 Y
is also a 2D array where each row is a query point and you need to have the same number of columns as X
. Y
还是2D数组,其中每一行都是一个查询点,并且您需要具有与X
相同的列数。 We would also specify the flag 'k'
to denote how many closest points you want returned. 我们还将指定标志
'k'
来表示您要返回的最近点数。 By default, k = 1
. 默认情况下,
k = 1
。
As such, idx
would be a N x K
matrix, where N
is the total number of query points (number of rows of Y
) and K
would be those k
closest points to the dataset for each query point we have. 这样,
idx
将是一个N x K
矩阵,其中N
是查询点的总数( Y
的行数),而K
将是我们拥有的每个查询点与数据集最接近的k
个点。 idx
indicates the particular points in your dataset that were closest to each query. idx
表示数据集中最接近每个查询的特定点。 d
is also a N x K
matrix that returns the smallest distances for these corresponding closest points. d
也是一个N x K
矩阵,它返回这些对应的最近点的最小距离 。
As such, what you want to do is find the closest point for your dataset to each of the other points, ignoring self-distances. 这样,您想要做的就是找到数据集与其他每个点的最接近点,而忽略自身距离。 Therefore, you would set both
X
and Y
to be the same, and set k = 2
, discarding the first column of both outputs to get the result you're looking for. 因此,您可以将
X
和Y
设置为相同,并设置k = 2
,丢弃两个输出的第一列以获得所需的结果。
Therefore: 因此:
[idx,d] = knnsearch(point_coordinates, point_coordinates, 'k', 2)
idx = idx(:,2);
d = d(:,2);
We thus get for idx
and d
: 因此,我们得到
idx
和d
:
>> idx
idx =
3
5
1
1
7
3
5
>> d
d =
17.3562
18.5316
17.3562
31.9027
13.7573
20.4624
13.7573
As such, this tells us that for the first point in your data set, it matched with point #3 the best. 因此,这告诉我们,对于数据集中的第一个点,它与第3点最匹配。 This matched with the closest distance of 17.3562.
这与最近的距离17.3562相匹配。 For the second point in your data set, it matched with point #5 the best with the closest distance being 18.5316.
对于数据集中的第二个点,它与点#5匹配得最好,最接近的距离是18.5316。 You can continue on with the rest of the results in a similar pattern.
您可以按照类似的方式继续处理其余结果。
If you don't have access to the statistics toolbox, consider reading my StackOverflow post on how I compute KNN from first principles. 如果您无权访问统计信息工具箱,请考虑阅读我的StackOverflow帖子,内容涉及如何根据第一原理计算KNN。
Finding K-nearest neighbors and its implementation 寻找K近邻及其实现
In fact, it is very similar to Luis Mendo's post to you earlier. 实际上,这与路易斯·门多(Luis Mendo)之前给您的帖子非常相似。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.