I am currently facing this problem while working on R.
I have a big matrix, say A, of size (M x K) and a smaller matrix, say B, of size (N x K), with of course N < M. So B has the same number of columns as A, but less rows.
A possible example can be:
A <- replicate(K, rnorm(M))
B <- replicate(K, sample(M, N))
My goal is to subset each column of A with the correspondent column of B in an efficent way, since the dimension of the matrices is huge.
A possible numerical example is:
A <- [1 2 3 B <- [1 1 3 ---> [1 2 9
4 5 6 3 2 2] 7 5 6]
7 8 9]
This means I am looking for something better than parsing column-by-column using a simple for loop:
for(i in 1:K)
{
A[B[,i],i]
}
Thank you for helping me to solve this apparently trivial, but for me challenging problem.