Skip to content
Prev 363413 / 398502 Next

optimize the filling of a diagonal matrix (two for loops)

?

The nested for-loops could very easily be moved to Rcpp which should speed them up. Using apply functions instead of for-loops will not make it faster; they still have to do the same looping.

At least, when I use `outer` to replace the loop I get roughly the same speed for the two versions ? although the `outer` solution does iterate over the entire matrix and not just the upper-triangular matrix.

library(stringdist) # I don?t have TSmining library installed so I tested with this instead
for_loop_test <- function() {
? matrixPrepared <- matrix(NA, nrow = nrow(dataS), ncol = nrow(dataS))
? for (i in 1:(nrow(dataS)-1)){
? ? for (j in (1+i):nrow(dataS)){
? ? ? matrixPrepared[i, j] <- stringdist(paste0(as.character(dataS[i,]), collapse=""),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?paste0(as.character(dataS[j,]), collapse=""))
? ? }
? }
? matrixPrepared
}

apply_test <- function() {
? get_dist <- function(i, j) {
? ? if (i <= j) NA
? ? else stringdist(paste0(as.character(dataS[i,]), collapse=""),
? ? ? ? ? ? ? ? ? ? paste0(as.character(dataS[j,]), collapse=""))
? }
? get_dist <- Vectorize(get_dist)
? t(outer(1:nrow(dataS), 1:nrow(dataS), get_dist))
}

library(microbenchmark)
equivalent <- function(x, y) (is.na(x) && is.na(y)) || (x == y)
check <- function(values) all(equivalent(values[[1]], values[[2]]))
microbenchmark(for_loop_test(), apply_test(), check = check, times = 5)

Cheers
	Thomas
On 18 August 2016 at 17:41:01, AURORA GONZALEZ VIDAL (aurora.gonzalez2 at um.es(mailto:aurora.gonzalez2 at um.es)) wrote: