-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
Description
The matrix approach is much faster than the sampling approach.
probabilities <- matrix(runif(1000000), 100000, 10)
probabilities_ <- probabilities - runif(100000 * 10)
ind_sapply <- sapply(1:nrow(probabilities), function(i) sample(1:10, 1, prob = probabilities[i,]))
ind_mat <- max.col(probabilities_, 'first')
table(ind_sapply)
table(ind_mat)
Unit: milliseconds
expr min lq mean
sapply(1:nrow(probabilities), function(i) sample(1:10, 1, prob = probabilities[i, ])) 469.872150 619.764793 656.780946
max.col(probabilities_, "first") 1.955379 2.209599 2.308426
median uq max neval cld
655.096765 686.15894 1047.475831 100 b
2.270803 2.36885 2.911497 100 a Reactions are currently unavailable