If res
if your main matrix and indexes
is the matrix of indices:
This could help:
idx <- do.call("c",apply(indexes,1,function(x){seq(x[1],x[2])}))
res[idx,] <- 1
As to timing, first create a large indices matrix:
> set.seed(42)
> indexes <- t(matrix(sort(sample(1:10000,1000)),2,500))
> head(indexes)
[,1] [,2]
[1,] 3 4
[2,] 14 16
[3,] 23 33
[4,] 40 63
[5,] 67 74
[6,] 79 83
and time them:
> system.time(idx <- do.call("c",apply(indexes,1,function(x){seq(x[1],x[2])}))) user system elapsed
0.008 0.000 0.007
> system.time( idx2 <- unlist( apply( indexes , 1 , FUN = function(x){ seq.int(x[1],x[2])}) ))
user system elapsed
0.004 0.000 0.002
It would appear that the second method is slightly faster.