Fast way to compute distance matrix in R for large matrix

2.3k Views Asked by At

I am working on R to compute the distance matrix for a large matrix. Matrix has 39900 rows and 1990 columns:

set.seed(123)
#Matrix
M <- matrix(rnorm(39900*1990),nrow = 39900,ncol = 1990)

The issue appears when I want to compute the distance matrix:

#Distance
d <- dist(M,method = 'euclidean')

Having a computer with icore3 processor and 8GB ram using R 64 bits, it has elapsed more than 24 hours and the matrix has not been computed yet.

Is there any way to boost the computing maybe using Rcpp or other method? I need to obtain the distance matrix and other solutions in this site have not contributed to solve the problem.

1

There are 1 best solutions below

2
Alex Reynolds On BEST ANSWER

Perhaps try the distances package: https://cran.r-project.org/web/packages/distances/distances.pdf

install.packages("distances")
library("distances")
set.seed(123)
M <- matrix(rnorm(39900*1990),nrow = 39900,ncol = 1990)
d <- distances(M)