简体   繁体   English

R:两个矩阵的列之间的成对欧几里得距离

[英]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,...422j=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.

相关问题 如何计算 R 中两个矩阵之间的欧几里得距离 - how to calculate Euclidean distance between two matrices in R 在大数据的两个矩阵之间的所有列之间应用配对欧几里德距离 - Applying paired Euclidean Distance between all columns between two matrices for Big Data 如何计算维数不相等的两个矩阵之间的R欧式距离 - How to calculate the euclidean distance in R between two matrices each with unequal dimensions (速度挑战)任何更快的方法来计算两个矩阵的行之间的距离矩阵,就欧几里得距离而言? - (Speed Challenge) Any faster method to calculate distance matrix between rows of two matrices, in terms of Euclidean distance? 如何计算矩阵中两个元素之间的最大欧几里得距离 - R? - How to calculate the max euclidean distance between two elements in a matrix - R? 成对距离两个矩阵R - Pairwise distance two matrix R 如何使用外部乘积计算R中的成对欧几里德距离 - How to use outer product to compute pairwise Euclidean distance in R 计算两个整数矩阵/数据帧的所有行之间的成对汉明距离 - Computing pairwise Hamming distance between all rows of two integer matrices/data frames 如何使用 R 计算两组点(坐标)之间的欧氏距离的房屋距离 - How to calculate House distance with euclidean distance between two set of points (coordinates) with R 计算 r 中组之间的欧式距离 - Calculate euclidean distance between groups in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM