r - Paste elements together pairwise -


let's have 2 vectors , distance matrix below

v1 = sample(c(0,1),5,replace=true) v2 = sample(c(0,1),5,replace=true) d = matrix(rep(1,5*5),ncol=5) diag(d) <- 0 

using function below i'm calculating distance

how paste them together

here first attempt removing inner loop , using vectorization vector2 multiplication , sum:

  f_d_categorical2 <- function(vector1, vector2, dist.matrix) {   ptm <- proc.time()   dist <- 0   (i in 1:length(vector1)) {       dist <- dist + sum(vector1[i]*vector2*dist.matrix[i,])   }   print(proc.time()-ptm)   return(dist) } 

process time went 1.8 0.03 sec. sure there room improvement , additional test cases.


Comments