vectorization - Vectorise R code to randomly select 2 columns from each row -
does have suggestions of how vectorise code or otherwise speed up? i'm creating matrix, potentially large. in each row, want select 2 columns @ random, , flip them 0 1.
i cannot select same row , column number, i.e. diagonal of matrix 0 hence (1:n)[-j] in sample(). because changes each row, can't see way using vectorisation, parallelisation option here?
i use library(matrix) sparse matrix functionality.
library(matrix) n <- 100 m <- matrix(0, nrow = n, ncol = n) for(j in 1:n) { cols <- sample((1:n)[-j], 2) #choose 2 columns not equal m[j, cols] <- 1 } any ideas?
library(matrix) n <- 7 desired_output <- matrix(0, nrow = n, ncol = n) set.seed(1) for(j in 1:n) { cols <- sample((1:n)[-j], 2) #choose 2 columns not equal desired_output[j, cols] <- 1 } # 7 x 7 sparse matrix of class "dgcmatrix" # # [1,] . . 1 . . . 1 # [2,] . . . . 1 1 . # [3,] . 1 . . . 1 . # [4,] . . . . 1 . 1 # [5,] 1 . . 1 . . . # [6,] 1 1 . . . . . # [7,] . 1 . . 1 . . res <- matrix(0, nrow = n, ncol = n) set.seed(1) ind <- cbind(rep(1:n, each = 2), c(sapply(1:n, function(j) sample((1:n)[-j], 2)))) res[ind] <- 1 all.equal(res, desired_output) # [1] true quick bench:
microbenchmark::microbenchmark( op = { desired_output <- matrix(0, nrow = n, ncol = n) set.seed(1) for(j in 1:n) { cols <- sample((1:n)[-j], 2) #choose 2 columns not equal desired_output[j, cols] <- 1 } }, aurele = { res <- matrix(0, nrow = n, ncol = n) set.seed(1) ind <- cbind(rep(1:n, each = 2), c(sapply(1:n, function(j) sample((1:n)[-j], 2)))) res[ind] <- 1 } ) # unit: milliseconds # expr min lq mean median uq max neval cld # op 10.240969 10.509384 11.065336 10.804949 11.044846 14.903377 100 b # aurele 1.185001 1.258037 1.392021 1.363503 1.434818 4.553614 100
Comments
Post a Comment