[英]R: pairwise Euclidean distance between columns of two matrices
The following loop takes too lonng to run (2mins/iteration) The tumor_signals is size 950000x422 The normal_signals is size 950000x772 Any ideas for how to speed it up?以下循环运行时间太长(2 分钟/迭代)tumor_signals 大小为 950000x422 normal_signals 大小为 950000x772 有关如何加快它的任何想法?
for(i in 1:ncol(tumor_signals)){
x <- as.vector(tumor_signals[,i])
print("Assigned x")
y <- t((t(normal_signals) - x)^2)
print("assigned y")
y <- t(sqrt(colSums(y)))
print("done")
#all_distance <- cbind(all_distance,matrix(distance))
print(i)
}
There's a bug in your code -- you don't need to take the transpose of normal_signals
.您的代码中有一个错误——您不需要转置
normal_signals
。 As I understand it, you are trying to compute, for all i = 1,2,...422
, and j=1,2,...,772
, the Euclidean distance between tumor_signals[,i]
and normal_signals[,j]
.据我了解,对于所有
i = 1,2,...422
和j=1,2,...,772
,您正在尝试计算tumor_signals[,i]
和normal_signals[,j]
. You would probably want the results in a 422 x 772 matrix.您可能希望结果为 422 x 772 矩阵。 There's a function
rdist()
in the package fields
that will do this for you: package
fields
中有一个 function rdist()
将为您执行此操作:
require(fields)
result <- rdist(t(tumor_signals), t(normal_signals))
Incidentally, a Google search for [R Euclidean distance]
would have easily found this package.顺便说一句,谷歌搜索
[R Euclidean distance]
很容易找到这个 package。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.