DKa*_*yan 6 r distance matrix euclidean-distance
我有一个整数向量vec1,我正在使用dist函数生成一个远程矩阵。我想获取距离矩阵中某个值的元素的坐标(行和列)。本质上,我想获得相距 d 距离的一对元素。例如:
vec1 <- c(2,3,6,12,17)
distMatrix <- dist(vec1)
# 1 2 3 4
#2 1
#3 4 3
#4 10 9 6
#5 15 14 11 5
Run Code Online (Sandbox Code Playgroud)
说,我对向量中相距 5 个单位的一对元素感兴趣。我想得到坐标 1 是行和坐标 2 是距离矩阵的列。在这个玩具示例中,我希望
coord1
# [1] 5
coord2
# [1] 4
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种有效的方法来获取这些值而不涉及将dist对象转换为矩阵或遍历矩阵?
距离矩阵是打包格式的下三角矩阵,其中下三角矩阵按列存储为一维向量。你可以通过
str(distMatrix)
# Class 'dist' atomic [1:10] 1 4 10 15 3 9 14 6 11 5
# ...
Run Code Online (Sandbox Code Playgroud)
即使我们调用dist(vec1, diag = TRUE, upper = TRUE),向量仍然相同;只有打印样式会发生变化。也就是说,无论你如何调用dist,你总是得到一个向量。
这个答案侧重于如何在 1D 和 2D 索引之间进行转换,以便您可以使用“dist”对象,而无需首先使用as.matrix. 如果你确实想让它成为一个矩阵,使用as.matrix 中dist2mat定义的函数对距离对象非常慢;如何让它更快?.
为这些索引变换编写矢量化的 R 函数很容易。我们只需要处理“越界”索引,NA应该返回的索引。
## 2D index to 1D index
f <- function (i, j, dist_obj) {
if (!inherits(dist_obj, "dist")) stop("please provide a 'dist' object")
n <- attr(dist_obj, "Size")
valid <- (i >= 1) & (j >= 1) & (i > j) & (i <= n) & (j <= n)
k <- (2 * n - j) * (j - 1) / 2 + (i - j)
k[!valid] <- NA_real_
k
}
## 1D index to 2D index
finv <- function (k, dist_obj) {
if (!inherits(dist_obj, "dist")) stop("please provide a 'dist' object")
n <- attr(dist_obj, "Size")
valid <- (k >= 1) & (k <= n * (n - 1) / 2)
k_valid <- k[valid]
j <- rep.int(NA_real_, length(k))
j[valid] <- floor(((2 * n + 1) - sqrt((2 * n - 1) ^ 2 - 8 * (k_valid - 1))) / 2)
i <- j + k - (2 * n - j) * (j - 1) / 2
cbind(i, j)
}
Run Code Online (Sandbox Code Playgroud)
这些函数在内存使用方面非常便宜,因为它们使用索引而不是矩阵。
finv于您的问题您可以使用
vec1 <- c(2,3,6,12,17)
distMatrix <- dist(vec1)
finv(which(distMatrix == 5), distMatrix)
# i j
#[1,] 5 4
Run Code Online (Sandbox Code Playgroud)
一般来说,距离矩阵包含浮点数。==用来判断两个浮点数是否相等是有风险的。阅读为什么这些数字不相等?更多和可能的策略。
dist2matdist2mat在距离对象上使用as.matrix 中给出的函数非常慢;如何让它更快?,我们可以使用which(, arr.ind = TRUE).
library(Rcpp)
sourceCpp("dist2mat.cpp")
mat <- dist2mat(distMatrix, 128)
which(mat == 5, arr.ind = TRUE)
# row col
#5 5 4
#4 4 5
Run Code Online (Sandbox Code Playgroud)
## 2D index to 1D index
The lower triangular looks like this: $$\begin{pmatrix} 0 & 0 & \cdots & 0\\ \times & 0 & \cdots & 0\\ \times & \times & \cdots & 0\\ \vdots & \vdots & \ddots & 0\\ \times & \times & \cdots & 0\end{pmatrix}$$ If the matrix is $n \times n$, then there are $(n - 1)$ elements ("$\times$") in the 1st column, and $(n - j)$ elements in the j<sup>th</sup> column. Thus, for element $(i,\ j)$ (with $i > j$, $j < n$) in the lower triangular, there are $$(n - 1) + \cdots (n - (j - 1)) = \frac{(2n - j)(j - 1)}{2}$$ "$\times$" in the previous $(j - 1)$ columns, and it is the $(i - j)$<sup>th</sup> "$\times$" in the $j$<sup>th</sup> column. So it is the $$\left\{\frac{(2n - j)(j - 1)}{2} + (i - j)\right\}^{\textit{th}}$$ "$\times$" in the lower triangular.
----
## 1D index to 2D index
Now for the $k$<sup>th</sup> "$\times$" in the lower triangular, how can we find its matrix index $(i,\ j)$? We take two steps: 1> find $j$; 2> obtain $i$ from $k$ and $j$.
The first "$\times$" of the $j$<sup>th</sup> column, i.e., $(j + 1,\ j)$, is the $\left\{\frac{(2n - j)(j - 1)}{2} + 1\right\}^{\textit{th}}$ "$\times$" of the lower triangular, thus $j$ is the maximum value such that $\frac{(2n - j)(j - 1)}{2} + 1 \leq k$. This is equivalent to finding the max $j$ so that $$j^2 - (2n + 1)j + 2(k + n - 1) \geq 0.$$ The LHS is a quadratic polynomial, and it is easy to see that the solution is the integer no larger than its first root (i.e., the root on the left side): $$j = \left\lfloor\frac{(2n + 1) - \sqrt{(2n-1)^2 - 8(k-1)}}{2}\right\rfloor.$$ Then $i$ can be obtained from $$i = j + k - \left\{\frac{(2n - j)(j - 1)}{2}\right\}.$$
Run Code Online (Sandbox Code Playgroud)