R fastest way to multiply rows of a matrix by rows of a vector

只谈情不闲聊 提交于 2019-12-10 16:24:12

问题


I want to multiply rows of a matrix by EACH row (element) of a vector, not the entire vector (as the other question already posted talks about.)

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

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

to output

1  2  3 
8  10 12
21 24 27

I need to do this VERY efficiently, as I need to do it with massive amounts of data thousands of times. I used

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

but this is much too slow.


回答1:


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


来源:https://stackoverflow.com/questions/22738884/r-fastest-way-to-multiply-rows-of-a-matrix-by-rows-of-a-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!