简体   繁体   中英

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?

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 . 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] . You would probably want the results in a 422 x 772 matrix. There's a function rdist() in the package fields that will do this for you:

require(fields)
result <- rdist(t(tumor_signals), t(normal_signals))

Incidentally, a Google search for [R Euclidean distance] would have easily found this package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM