Find the euclidean distance between elements of two matrices, one element of first one from all elements of the second one.
    4 ビュー (過去 30 日間)
  
       古いコメントを表示
    
I have
{(x1,y1)|x1 belongs to X1 and y1 belongs to Y1}
{(x2,y2)|x2 belongs to X2 and y2 belongs to Y2}
and:
X1=[68.0178   75.0520   51.1820   35.4534
  152.7847  158.3533  108.2593  197.1259
  241.1594  255.1793  271.9570  234.6449
  360.2638  358.3571  399.6156  388.6544]
Y1=[45.4695  130.8915  200.9802  304.2660
   41.3427  172.6104  284.3213  337.8186
   21.7732  178.2872  292.2332  370.4340
   12.5655  169.3788  277.0954  372.9513]
X2=[22.4277   67.3031
  226.9055  247.7492]
Y2=[ 62.3716  217.7124
   23.6445  282.9643]
------------------------------------------
I want to get the distances between each one of (x1,y1)s from all of (x2,y2)s. (x1,y1), (x2,y2) are coordinates of some points.
0 件のコメント
採用された回答
  John Chilleri
      
 2017 年 2 月 9 日
        
      編集済み: John Chilleri
      
 2017 年 2 月 9 日
  
      Hello,
One simple approach:
for ind1 = 1:size(X2,1)
   for ind2 = 1:size(X2,2)
      for ind3 = 1:size(X1,1)
         for ind4 = 1:size(X1,2)
            % Compute distance between (X1(ind3,ind4),Y1(ind3,ind4)) 
            %                      and (X2(ind1,ind2),Y2(ind1,ind2))
         end
      end
   end
end
Or you could pursue a more efficient approach using the aformentioned pdist2:
X1col = reshape(X1,numel(X1),1);
Y1col = reshape(Y1,numel(Y1),1);
X2col = reshape(X2,numel(X2),1);
Y2col = reshape(Y2,numel(Y2),1);
Distances = pdist2([X1col Y1col],[X2col Y2col])
Which results with,
>> Distances
Distances =
   48.6224  160.3796  172.2444  297.8375
  132.0423   76.2045  195.9933  259.6137
  222.4675   14.3762  261.9507  261.2742
  341.4877  133.8177  357.6468  292.8738
   86.3961  185.9070   87.1660  230.1097
  175.0096  163.9824  101.6087  142.0198
  260.0187  157.2061  191.9683  104.9405
  352.5608  196.2601  295.0399  158.5427
  141.5597  249.6532   23.2348  212.9790
  237.9679  286.4076   78.1931  139.4965
  339.2657  272.3408  217.7994   25.9216
  434.0242  306.7020  337.5766  151.9798
  242.2449  339.7092   92.2276  213.3618
  326.1756  315.5823  176.8600   74.6439
  374.0837  346.8759  226.5550   88.4459
  480.1893  384.9389  356.8834  167.1883
where the first column of distances corresponds to your first (x2,y2) point and displays the distances between (x11,y11),(x21,y21),etc. In other words, the first 4 elements of the first column are the distances between your first (x2,y2) point and the first column of (x1,y1) and the next 5-8 elements are the distances with the second column of points in (x1,y1), etc.
Note: my answer has been altered in accordance with Star Strider's comment. Thank you for the pdist2 over pdist recommendation, it was absolutely preferable.
I hope this helps!
3 件のコメント
その他の回答 (0 件)
参考
カテゴリ
				Help Center および File Exchange で Statistics and Machine Learning Toolbox についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!