R is the fastest way to multiply matrix rows by vector rows

I want to multiply the rows of the matrix by EVERY row (element) of the vector, and not the whole vector (as another question has already said).

for example, I want to use these two matrices (or oo is a vector, since it is one column)

oo=matrix(1:3,3,1)
oop=matrix(1:9,3,3,byrow=TRUE)

for conclusion

1  2  3 
8  10 12
21 24 27

I need to do this VERY efficiently, since I need to do this with a huge amount of data thousands of times. I used

diag(as.vector(oo))%*%oop

but it is too slow.

+4
source share
1 answer
R>oop*drop(oo)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    8   10   12
[3,]   21   24   27
+4
source

Source: https://habr.com/ru/post/1534090/


All Articles