简体   繁体   English

配对最短距离

[英]sort pairwise shortest distance

This is my problem. 这是我的问题。

clc; clear all; close all;  
N = 10;
R = randn(N,1)+10;R(end) = R(1);
tht = linspace(0,2*pi,N).';
x = R.*cos(tht);
y = R.*sin(tht);
plot(x, y,'o-b');

Randomly sorting the arrays 随机排序数组

X = x(randperm(size(x,1)),:);
Y = y(randperm(size(y,1)),:);
hold on, plot(X,Y,'o-r');

As it can be seen the contour that is drawn has overlapping regions. 可以看出,绘制的轮廓具有重叠的区域。 so I wanted to draw a non-overlapping closed contour. 所以我想画一个不重叠的闭合轮廓。 One idea that I got was by ordering the elements of the matrix such that the adjacent distances between the elements of the matrix are minimum. 我得到的一个想法是对矩阵的元素进行排序,以使矩阵元素之间的相邻距离最小。 So the closest points will be adjacent to one another. 因此,最接近的点将彼此相邻。

Can anyone specify how I can do it ? 谁能指定我该怎么做? I tried to use pdist2 but failed. 我尝试使用pdist2,但失败了。

If I understand you correctly, you want to reorder some vertices such that all the lines drawn between subsequent points form a closed, non-overlapping contour. 如果我对您的理解正确,则希望对某些顶点进行重新排序,以使后续点之间绘制的所有线形成闭合的,不重叠的轮廓。

This can be accomplished by re-ordering your vertices in a (counter)clockwise fashion about the centre of mass of all the points. 这可以通过围绕所有点的质心以(逆时针)方式对顶点重新排序来实现。 In 2D, this can most easily be accomplished by sorting the output of atan2 : 在2D模式下,最简单的方法是对atan2的输出进行排序:

%// Compute centre of mass
r_COM = sum([X, Y]) / numel(X);

%// Sort all vertices by angle
[~, I] = sort(atan2(Y - r_COM(2), X - r_COM(1)));

%// Plot the new contour
hold on, plot(X([I; I(1)]),Y([I; I(1)]), '.-k', 'linewidth', 2);

Results: 结果:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM