我想知道如何从距离矩阵中提取第一个对角线的值。
例如:
> mymatrix
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 6 4
[4,] 8 6
> dist(mymatrix)
1 2 3
2 2.828427
3 5.385165 3.000000
4 8.062258 5.385165 2.828427
我想获取向量中的值: 2.828427, 3.000000, 2.828427
谢谢!
一种解决方法是将dist
对象转换为matrix
行,然后提取其中行索引比列索引大一的元素:
mat = as.matrix(dist(mymatrix))
mat[row(mat) == col(mat) + 1]
# [1] 2.828427 3.000000 2.828427
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句