Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: optimal way of computing the "product" of two vectors

Tags:

r

Let's assume that I have a vector

r <- rnorm(4)

and a matrix W of dimension 20000*200 for example:

W <- matrix(rnorm(20000*200),20000,200)

I want to compute a new matrix M of dimension 5000*200 such that m11 <- r%*%W[1:4,1], m21 <- r%*%W[5:8,1], m12 <- r%*%W[1:4,2] etc. (i.e. grouping rows 4-by-4 and computing the product).

What's the optimal (speed,memory) way of doing this?

Thanks in advance.

like image 806
teucer Avatar asked Dec 23 '22 02:12

teucer


1 Answers

This seems to run fastest for me:

array(r %*% array(W, c(4, 20000 * 200 / 4)), c(5000, 200))
like image 110
Jonathan Chang Avatar answered Jan 22 '23 07:01

Jonathan Chang