【发布时间】:2015-03-21 20:25:53
【问题描述】:
我正在尝试计算 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]
我不确定如何构造“for”循环/nearest_neighbour 线以获取每个 u,v 向量的最近邻距离。
我想拥有,例如; 对于第一个向量,你可以有坐标和相应的最短距离,对于第二个向量,你可以有另一个最短距离,这一直持续到 n
希望有人能提供帮助。
谢谢
【问题讨论】:
标签: matlab for-loop matrix vector distance