簡體   English   中英

如何從質心計算多維空間中的點距離

[英]How can I calculate distance of points in multi-dimension space from the centroid

我在5維中具有質心,質心給出為centroid<-c(-0.0160560385542169, -0.0125691807228916, -0.000605079518072289, -0.00174781445783133, -0.0199511518072289) 我還有一個包含5個樣本的矩陣,其中PCA1:PCA5代表這些樣本的5個點的維數。 如何從質心計算每個樣本的歐幾里得距離? 如果這是從一個點到另一個點的距離,我可以簡單地完成dist(mymat[,-1]) ,但是我不確定如何獲得質心的距離。 有人可以建議嗎?

mymat<- structure(c("10687:G41F", "10687:SKDP-225.3", "10687:2671", "10687:LPH-001-16_SCC", 
"10687:MC1R-694CB-T", "-0.0039950", "-0.0203415", "-0.0200395", 
"-0.0147320", "-0.0196970", "-0.0140180", "-0.0181240", "-0.0165090", 
"-0.0148700", "-0.0170765", "-0.0136615", "-0.0010915", "-0.0014500", 
" 0.0020240", "-0.0021095", "-0.0002395", "-0.0019710", "-0.0017595", 
" 0.0036180", "-0.0036255", "-0.0184015", "-0.0197400", "-0.0238185", 
"-0.0282375", "-0.0323130"), .Dim = 5:6, .Dimnames = list(c("1", 
"2", "3", "4", "5"), c("samples", "PCA1", "PCA2", "PCA3", 
"PCA4", "PCA5")))

dist應該概括為您想要的任何尺寸...

centroid <- c(-0.0160560385542169, -0.0125691807228916, -0.000605079518072289, 
 -0.00174781445783133, -0.0199511518072289)
rbind(c("centroid", centroid), mymat) -> k
dist(k, "euclidean") -> dd
as.matrix(dd) -> dd
k[,1] -> rownames(dd) 
as.data.frame(dd)
dd[2:6,1] -> dist_to_centroid
View(as.data.frame(dist_to_centroid))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM