在Matlab中对坐标点进行排序 [英] Sort Coordinates Points in Matlab

查看:790
本文介绍了在Matlab中对坐标点进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是对这些坐标点进行排序:

What I want to do is to sort these coordinates points:

测量坐标(x,y)=(2,2),(2,3),(1,2),(1,3),(2,1),(1,1),(3, 2),(3,3),(3,1)

Measured coordinates (x,y)= (2,2),(2,3),(1,2),(1,3),(2,1),(1,1),(3,2),(3,3),(3 ,1)

我需要获取这些点的序列或轨迹以通过迭代跟随它们.

I need to get sequences or trajectories of this points to follow them by iteration.

推荐答案

data = [2,2 ; 2,3 ; 1,2 ; 1,3 ; 2,1 ; 1,1 ; 3,2 ; 3,3 ; 3 ,1]
% corresponding sort-value, pick one out or make one up yourself:
sortval = data(:,1); % the x-value
sortval = data(:,2); % y-value
sortval = (data(:,1)-x0).^2 + (data(:,2)-y0).^2; % distance form point (xo,y0)
sortval = ...

[~,sortorder] = sort(sortval);
sorted_data = data(sortorder,:);

但是从您的评论中,我了解到您实际上需要一些东西来重建路径,并迭代找到最后找到的点(到目前为止已重建的路径)的最近邻居.

But from you comment, I understand you actually need something to reconstruct a path and iteratively find the closest neighbour of the last found point (of the reconstructed path so far).

以下是我如何解决此问题的方法(使用pdist2来计算所有点之间的距离以简化操作):

The following is how I would solve this problem (using pdist2 for calculating the distances between all the points for easiness):

data = [2,2 ; 2,3 ; 1,2 ; 1,3 ; 2,1 ; 1,1 ; 3,2 ; 3,3 ; 3 ,1];
dist = pdist2(data,data);

N = size(data,1);
result = NaN(1,N);
result(1) = 1; % first point is first row in data matrix

for ii=2:N
    dist(:,result(ii-1)) = Inf;
    [~, closest_idx] = min(dist(result(ii-1),:));
    result(ii) = closest_idx;
end

其结果是:

result =
     1     2     4     3     6     5     9     7     8

是曲线上连续点的索引.这是此结果的图解:

being the indices to consecutive points on the curve. Here's a plot of this result:

正如@ mathematician1975已经提到的,到一个点的距离可以相等.这可以通过使用min来解决,该方法仅查找数组中最小值的首次出现.这意味着,如果您对输入数据进行不同的排序,则当然可以得到不同的结果,这是等距离问题所固有的.

As @mathematician1975 already mentioned, there can be equal distances to a point. This is solved here by using min which just finds the first occurrence of the minimum in an array. This means that if you order your input data differently, you can get different results of course, this is inherent to the equal-distance issue.

第二个评论:我不知道在使用大输入数据矩阵时会如何表现,由于循环,这可能会有点慢,这是您无法避免的.我仍然看到需要改进的地方,但这取决于您;)

2nd remark: I don't know how this will behave when using large input data matrices, probably a bit slow because of the loop, which you can't avoid. I still see room for improvement, but that's up to you ;)

这篇关于在Matlab中对坐标点进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆