I have two matrices, one with values of interest and the other with indices corresponding to columns in the first. The code below does what I want but it will need to be run on much larger matrices and many many times. My gut tells me there might be a faster way to accomplish this so any thoughts would be appreciated.
set.seed(0)
y = matrix(runif(20), 4, 5)
idx = matrix(sample(1:5, 12, replace = T), 4, 3)
z = lapply(1:nrow(y), function(i) y[i, idx[i,]])
z = do.call(rbind, z)