获得独特的点对

时间:2014-04-17 18:21:38

标签: matlab

我正在从一组点计算距离函数,以选择具有距离x的一定公差d的点,并编写此代码:

function pts_pairs = donut_neighbor(pts,x,d)
% Matrix of pts repeated for the total number of points 
temp_pts1 = repmat(pts,size(pts,1),1);
% Matrix of pts where each row is repeated total number of point times
% Uses kronecker product which repeats all the elements 
temp_pts2 = kron(pts,ones(size(pts,1),1));
% Compute the distance between the matrices 
dist = sqrt((temp_pts1(:,1)-temp_pts2(:,1)).^2 + (temp_pts1(:,2)-temp_pts2(:,2)).^2);
% Get indices of the point pairs in the donut
ind = dist > (x-d/2) & dist < (x+d/2);
% output point coordinates of the point pairs
pts_pairs = [temp_pts1(ind,:) temp_pts2(ind,:)];

现在我想获得唯一的点对。因此,对于我的代码,点对A-B将被计为两次作为A-B和B-A,但我只想对A-B对进行计数(另一对要被擦除)。有什么简单的方法吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我假设你有类似的东西:

pts_pairs =

     1     2
     1     3
     3     4
     2     1
     3     1
     4     5

例如,如果1与2相关,而2与1相关,则只想保留一次。你可以这样做:

unique(sort(pts_pairs, 2), 'rows')

给出了:

ans =

     1     2
     1     3
     3     4
     4     5