R中数据集之间的欧几里德距离,使用“rdist from”;“字段”;包裹

R中数据集之间的欧几里德距离,使用“rdist from”;“字段”;包裹,r,matrix,euclidean-distance,R,Matrix,Euclidean Distance,我使用rdist计算矩阵与自身之间的欧几里德距离: > m = matrix(c(1,1,1,2,2,2,3,4,3),nrow=3, ncol=3) > m [,1] [,2] [,3] [1,] 1 2 3 [2,] 1 2 4 [3,] 1 2 3 library(fields) > rdist(m) [,1] [,2] [,3] [1,] 1e-10 1e+00 1e-10 [2,] 1e+

我使用
rdist
计算矩阵与自身之间的欧几里德距离:

> m = matrix(c(1,1,1,2,2,2,3,4,3),nrow=3, ncol=3)
> m
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    4
[3,]    1    2    3
library(fields)
> rdist(m)
      [,1]  [,2]  [,3]
[1,] 1e-10 1e+00 1e-10
[2,] 1e+00 1e-10 1e+00
[3,] 1e-10 1e+00 1e-10
让我困惑的是,我认为对角线上应该有0(当然向量到自身的距离是0?),出于同样的原因,在比较第一行和第三行时应该有0。相反,我看到的值(1e-10)看起来非常大,是数值噪声。怎么了


编辑:
rdist
来自包
字段

首先
1e-10
只是
1*10^-10
,即
0.0000000001
,因此在数字上非常接近
0
(因为它是平方根的结果,所以计算中的实际误差是一行大小
1e-20
)。是不是太大了?嗯,这个库是用fortran编写的,它注重速度,所以它是可以接受的。如果您分析准确的代码,您将了解它是如何计算的:

# fields, Tools for spatial data
# Copyright 2004-2011, Institute for Mathematics Applied Geosciences
# University Corporation for Atmospheric Research
# Licensed under the GPL -- www.gpl.org/licenses/gpl.html
"rdist" <- function(x1, x2) {
    if (!is.matrix(x1)) 
        x1 <- as.matrix(x1)
    if (missing(x2)) 
        x2 <- x1
    if (!is.matrix(x2)) 
        x2 <- as.matrix(x2)
    d <- ncol(x1)
    n1 <- nrow(x1)
    n2 <- nrow(x2)
    par <- c(1/2, 0)
    temp <- .Fortran("radbas", nd = as.integer(d), x1 = as.double(x1), 
        n1 = as.integer(n1), x2 = as.double(x2), n2 = as.integer(n2), 
        par = as.double(par), k = as.double(rep(0, n1 * n2)))$k
    return(matrix(temp, ncol = n2, nrow = n1))
}

它将小的(甚至是0)值视为
1e-20
,取平方根后得到
1e-10
。似乎动机是通过使用值的对数来加速过程(因此,平方根简单地除以2),这当然不是为
0
定义的,首先
1e-10
只是
1*10^-10
,这是
0.0000000001
,因此在数字上非常接近
0
(因为它是平方根的结果,所以计算中的实际误差是一行量级的
1e-20
)。它是否“太大”?好吧,这个库是用fortran编写的,并且关注速度,所以它是可以接受的。如果你分析精确的代码,你会发现它是如何计算的:

# fields, Tools for spatial data
# Copyright 2004-2011, Institute for Mathematics Applied Geosciences
# University Corporation for Atmospheric Research
# Licensed under the GPL -- www.gpl.org/licenses/gpl.html
"rdist" <- function(x1, x2) {
    if (!is.matrix(x1)) 
        x1 <- as.matrix(x1)
    if (missing(x2)) 
        x2 <- x1
    if (!is.matrix(x2)) 
        x2 <- as.matrix(x2)
    d <- ncol(x1)
    n1 <- nrow(x1)
    n2 <- nrow(x2)
    par <- c(1/2, 0)
    temp <- .Fortran("radbas", nd = as.integer(d), x1 = as.double(x1), 
        n1 = as.integer(n1), x2 = as.double(x2), n2 = as.integer(n2), 
        par = as.double(par), k = as.double(rep(0, n1 * n2)))$k
    return(matrix(temp, ncol = n2, nrow = n1))
}

它将小的(甚至是0)值视为
1e-20
,取平方根后得到
1e-10
。其动机似乎是通过使用值的对数来加快过程(因此,平方根简单地除以2),当然不是为
0

rdist
定义的。就我所知,它不是一个基本的R函数。请告诉我们您正在使用哪个软件包。@VictorK.-请参阅编辑。
rdist
就我所知不是一个基本的R函数。请告诉我们您正在使用哪个软件包。@VictorK.-请参阅编辑。无需如此合作我忘记了平方根,现在误差的大小更有意义了。屈尊?我只是解决了所有潜在的问题,因为它(对我来说)不清楚,哪个部分导致了问题。在澄清后删除了多余的信息。添加了基于fortran Source的更精确的答案我在这个答案中没有看到任何屈尊俯就,也没有必要如此屈尊俯就。忘记了平方根,现在错误的大小更有意义了。屈尊俯就?我只是解决了所有可能的问题tial问题,因为(对我来说)不清楚是哪一部分导致了问题。在澄清后删除了多余的信息。添加了基于fortran Source的更精确的答案。我也看不到这个答案中有任何居高临下的地方