r - Ways to improve for loop for matrix manipulations depending on another matrix -


i know improving loop has been asked tons of times before. can apply family functions improve loop in r.

however there way improve manipulations of matrix manipulations depend on matrix? mean here this, elements set 2 in test based on matrix index:

for (i in 1:nrow(test)){   test[i,index[i,]]  <- 2 }    # index predetermined matrix 

another example this, set values in test based on ordering of elements in rows of matrix anymatrix:

for (i in 1:nrow(test)){    test[i,] <- order(anymatrix[i,]) } 

i use lapply or sapply here return list , takes same amount of time convert matrix.

reproducible example:

test <- matrix(0, nrow = 10, ncol = 10) set.seed(1234) index <- matrix(sample.int(10, 10*10, true), 10, 10) anymatrix <- matrix(rnorm(10*10), nrow = 10, ncol = 10)  (i in 1:nrow(test)){   test[i,index[i,]]  <- 2 }  (i in 1:nrow(test)){    test[i,] <- order(anymatrix[i,]) } 

you appear have 2 separate problems here.

problem 1: given matrix index, each row i , column j want set test[i,j] 2 if j appears in row i of index. can done simple matrix indexing, passing 2-column matrix of indices first column rows of elements want index , second column columns of elements want index:

test[cbind(as.vector(row(index)), as.vector(index))] <- 2 test #       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #  [1,]    2    2    0    2    2    2    2    0    2     2 #  [2,]    2    0    2    2    2    2    2    0    2     2 #  [3,]    2    2    2    2    0    0    2    2    0     0 #  [4,]    2    2    0    0    0    2    2    2    0     2 #  [5,]    2    2    2    2    0    0    0    0    2     0 #  [6,]    0    0    0    0    0    2    2    2    2     0 #  [7,]    2    0    2    2    2    2    2    0    0     0 #  [8,]    2    0    2    2    2    2    0    2    0     2 #  [9,]    2    2    2    2    0    0    2    0    2     2 # [10,]    2    0    2    0    0    2    2    2    2     0 

since operations in single vectorized operation, should faster looping through rows , handling them individually. here's example 1 million rows , 10 columns:

op <- function(test, index) {   (i in 1:nrow(test)){     test[i,index[i,]]  <- 2   }   test } josliber <- function(test, index) {   test[cbind(as.vector(row(index)), as.vector(index))] <- 2   test } test.big <- matrix(0, nrow = 1000000, ncol = 10) set.seed(1234) index.big <- matrix(sample.int(10, 1000000*10, true), 1000000, 10) identical(op(test.big, index.big), josliber(test.big, index.big)) # [1] true system.time(op(test.big, index.big)) #    user  system elapsed  #   1.564   0.014   1.591  system.time(josliber(test.big, index.big)) #    user  system elapsed  #   0.408   0.034   0.444  

here, vectorized approach 3.5x faster.

problem 2: want set row i of test order applied corresponding row of anymatrix. can apply:

(test <- t(apply(anymatrix, 1, order))) #       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #  [1,]    1   10    7    8    4    5    3    6    2     9 #  [2,]    8    7    1    6    3    4    9    5   10     2 #  [3,]    4    9    7    1    3    2    6   10    5     8 #  [4,]    1    2    6    4   10    3    9    8    7     5 #  [5,]    9    6    5    1    2    7   10    4    8     3 #  [6,]    9    3    8    6    5   10    1    4    7     2 #  [7,]    3    7    2    5    6    8    9    4    1    10 #  [8,]    9    8    1    3    4    6    7   10    5     2 #  [9,]    8    4    3    6   10    7    9    5    2     1 # [10,]    4    1    9    3    6    7    8    2   10     5 

i wouldn't expect of change in runtime here, because apply looping through rows how looping in solution. still, prefer solution because it's deal less typing , more "r" way of doing things.

note both of these applications used pretty different code, pretty typical in r data manipulation -- there lot of different specialized operators , need pick 1 that's right task. don't think there's single function or small set of functions going able handle matrix manipulations manipulation based on data matrix.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -