-2

I have a matrix call res initially like the following:

      [,1] [,2]
[1,]     0    0
[2,]     0    0
[3,]     0    0
[4,]     0    0
[5,]     0    0
[6,]     0    0
[7,]     0    0
[8,]     0    0
[9,]     0    0
[10,]    0    0 

I have a matrix of indexes (indexes) like the following:

     [,1] [,2]
 [1,]   2    3
 [2,]   7    9

I want the resulting res matrix to be like the following:

      [,1] [,2]
[1,]     0    0
[2,]     1    1
[3,]     1    1
[4,]     0    0
[5,]     0    0
[6,]     0    0
[7,]     1    1
[8,]     1    1
[9,]     1    1
[10,]    0    0 

I have a big matrix, it takes a long time to loop through the indexes matrix. Please let me know if there is a better approach to do this. I am hoping to do something like mat[indexes,] <- 1. However, this does not work I wanted.

user1938809
  • 1,135
  • 1
  • 9
  • 12

3 Answers3

2

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.

harkmug
  • 2,725
  • 22
  • 26
  • The 8th row does not contain ones in this case. It seems mat[array(indexes),] <- 1 and mat[indexes,] <- 1 are the same. – user1938809 Jun 18 '13 at 21:10
  • But your example index matrix does not have the number 8 in it (only 2,3,7,9), as far as I can see. – harkmug Jun 18 '13 at 21:13
  • That's the point of the problem. If you look at the resulting matrix. It clears indicates it. Thanks. – user1938809 Jun 18 '13 at 21:15
  • Use `head` or `dput` to post part of the actual `res` and `index` matrices to clarify your question. See: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – harkmug Jun 18 '13 at 21:17
  • Can you kindly provide an example in this case? Thanks. – user1938809 Jun 18 '13 at 21:20
  • I have tried idx <- unlist( apply( x , 1 , FUN = function(x){ seq.int(x[1],x[2])}) ) before. Which one is faster in your opinion? It seems Sometimes R runs out of memory if the matrix is good big. Any advice. Thanks. – user1938809 Jun 18 '13 at 22:32
0

EDIT: I did misunderstand, this should help:

test <- matrix(rep(0,1E7), ncol=2)
Index <- matrix(sort(sample(1:(1E7*0.5), size=10000)), ncol=2, byrow=TRUE)
test[unlist(apply(Index, 1, function(x){x[1]:x[2]})),] <- 1
rbatt
  • 4,677
  • 4
  • 23
  • 41
  • @user1938809 I've tried to reinterpret your question, and have edited my answer appropriately. If this isn't right, maybe you could clarify your problem. – rbatt Jun 18 '13 at 21:28
  • First, it gives error. Second, I want to have rows 2:3 and 7:9 to set to 1. It is easy to set row 2,3,7,9 to 1 with one call. The tricky part is the indexes matrix gives the starting and ending indexes for res matrix. Thanks. – user1938809 Jun 18 '13 at 21:36
  • @user1938809 Sorry for the error, there was a typo. I've updated the possible solution. – rbatt Jun 18 '13 at 22:11
  • Thanks. Is there a way to avoid apply as well? – user1938809 Jun 18 '13 at 22:39
0

Use an answer to your previous question to create a vector of row indexes ridx, and then

res[as.logical(ridx),] = 1L
Community
  • 1
  • 1
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
  • Yeah, that works but sometimes R runs out of memory. I was hoping if there is a more straight forward way. – user1938809 Jun 18 '13 at 22:07
  • @user1938809 be more specific -- when creating ridx? doing the subset replacement? how much memory do you have and how large (object.size()) are your objects? The operation above should not be particularly memory intensive compared to other solutions. – Martin Morgan Jun 18 '13 at 22:24