2014-04-17 46 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

不客氣。 :) –