如何使用外积计算 R 中的成对欧氏距离
How to use outer product to compute pairwise Euclidean distance in R
我有一个矩阵,前 10 行是质心,后 10 行是点。我想计算成对的欧氏距离。
mat = matrix(c(25,125, 44,105, 29,97, 35,63, 55,63,
42,57, 23,40, 64,37, 33,22, 55,20,
28,145, 65,140, 50,130, 38,115, 55,118,
50,90, 43,83, 63,88, 50,60, 50,30),
ncol=2, byrow=T)
centroids = mat[1:10,]
points = mat[11:20,]
eudis = function(x, y) { sqrt( sum( (x-y)^2 ) ) } # define Euclidean distance
我尝试像下面这样应用外积。
outer(centroids, points, FUN = eudis)
但是不行。我认为我在处理类似情况的外部产品方面取得了成功。有人能让它工作吗?提前致谢!
我想你想要一个矩阵来显示每个质心和每个点之间的距离。要使用 outer
做到这一点,您可以遍历索引:
outer(1:10, 1:10, FUN=Vectorize(function(x, y) eudis(centroids[x,], points[y,])))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 20.22375 42.72002 25.49510 16.40122 30.80584 43.01163 45.69464 53.03772 69.641941 98.23441
[2,] 43.08132 40.81666 25.70992 11.66190 17.02939 16.15549 22.02272 25.49510 45.398238 75.23962
[3,] 48.01042 56.08030 39.11521 20.12461 33.42155 22.13594 19.79899 35.17101 42.544095 70.21396
[4,] 82.29824 82.63776 68.65858 52.08647 58.52350 30.88689 21.54066 37.53665 15.297059 36.24914
[5,] 86.33076 77.64664 67.18631 54.70832 55.00000 27.45906 23.32381 26.24881 5.830952 33.37664
[6,] 89.10668 86.12781 73.43705 58.13777 62.36986 33.95585 26.01922 37.44329 8.544004 28.16026
[7,] 105.11898 108.46197 93.96276 76.48529 84.30896 56.82429 47.42362 62.48200 33.600595 28.79236
[8,] 113.84200 103.00485 94.04786 82.21922 81.49847 54.81788 50.56679 51.00980 26.925824 15.65248
[9,] 123.10158 122.26201 109.32978 93.13431 98.48858 70.09280 61.81424 72.49828 41.629317 18.78829
[10,] 127.88276 120.41595 110.11358 96.50907 98.00000 70.17834 64.13267 68.46897 40.311289 11.18034
确实,您需要对 eudis
函数进行矢量化处理,因为矢量是由 outer
传递给函数的,但不是 eudis
期望的方式。
我有一个矩阵,前 10 行是质心,后 10 行是点。我想计算成对的欧氏距离。
mat = matrix(c(25,125, 44,105, 29,97, 35,63, 55,63,
42,57, 23,40, 64,37, 33,22, 55,20,
28,145, 65,140, 50,130, 38,115, 55,118,
50,90, 43,83, 63,88, 50,60, 50,30),
ncol=2, byrow=T)
centroids = mat[1:10,]
points = mat[11:20,]
eudis = function(x, y) { sqrt( sum( (x-y)^2 ) ) } # define Euclidean distance
我尝试像下面这样应用外积。
outer(centroids, points, FUN = eudis)
但是不行。我认为我在处理类似情况的外部产品方面取得了成功。有人能让它工作吗?提前致谢!
我想你想要一个矩阵来显示每个质心和每个点之间的距离。要使用 outer
做到这一点,您可以遍历索引:
outer(1:10, 1:10, FUN=Vectorize(function(x, y) eudis(centroids[x,], points[y,])))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 20.22375 42.72002 25.49510 16.40122 30.80584 43.01163 45.69464 53.03772 69.641941 98.23441
[2,] 43.08132 40.81666 25.70992 11.66190 17.02939 16.15549 22.02272 25.49510 45.398238 75.23962
[3,] 48.01042 56.08030 39.11521 20.12461 33.42155 22.13594 19.79899 35.17101 42.544095 70.21396
[4,] 82.29824 82.63776 68.65858 52.08647 58.52350 30.88689 21.54066 37.53665 15.297059 36.24914
[5,] 86.33076 77.64664 67.18631 54.70832 55.00000 27.45906 23.32381 26.24881 5.830952 33.37664
[6,] 89.10668 86.12781 73.43705 58.13777 62.36986 33.95585 26.01922 37.44329 8.544004 28.16026
[7,] 105.11898 108.46197 93.96276 76.48529 84.30896 56.82429 47.42362 62.48200 33.600595 28.79236
[8,] 113.84200 103.00485 94.04786 82.21922 81.49847 54.81788 50.56679 51.00980 26.925824 15.65248
[9,] 123.10158 122.26201 109.32978 93.13431 98.48858 70.09280 61.81424 72.49828 41.629317 18.78829
[10,] 127.88276 120.41595 110.11358 96.50907 98.00000 70.17834 64.13267 68.46897 40.311289 11.18034
确实,您需要对 eudis
函数进行矢量化处理,因为矢量是由 outer
传递给函数的,但不是 eudis
期望的方式。