2014-04-17 47 views
0

我从一个点集计算距离函数与距离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进行计数(其他对将被擦除)。任何简单的方法去解决它?谢谢。

+0

使用'unique' ... – bla

+0

你能给输入和期望的输出最小的例子吗? –

回答

0

我假设你有这样的事情:

pts_pairs = 

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

例如,如果1与2,和2与1,你想只保留一个ocurrence。你可以做这样的事情:

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

其中给出:

ans = 

    1  2 
    1  3 
    3  4 
    4  5 
+0

工作。谢谢。 – shunyo

+0

不客气。 :) –