R:两个矩阵的列之间的成对欧几里德距离
以下循环运行时间太长(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)
}
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?
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)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有一个错误 - 您不需要对
normal_signals
进行转置。据我了解,您正在尝试计算所有i = 1,2,...422
和j=1,2,...,772
,tumor_signals[,i]
和normal_signals[,j]
之间的欧几里德距离。您可能希望结果采用 422 x 772 矩阵形式。fields
包中有一个函数rdist()
可以为您执行此操作:顺便说一句,Google 搜索
[R Euclidean distance]
会很容易找到这个包。There's a bug in your code -- you don't need to take the transpose of
normal_signals
. As I understand it, you are trying to compute, for alli = 1,2,...422
, andj=1,2,...,772
, the Euclidean distance betweentumor_signals[,i]
andnormal_signals[,j]
. You would probably want the results in a 422 x 772 matrix. There's a functionrdist()
in the packagefields
that will do this for you:Incidentally, a Google search for
[R Euclidean distance]
would have easily found this package.