R将矩阵的行乘以向量的行的最快方法 [英] R fastest way to multiply rows of a matrix by rows of a vector
本文介绍了R将矩阵的行乘以向量的行的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将矩阵的行乘以向量的每个行(元素),而不是整个向量(正如已经发布的另一个问题所讨论的那样).
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.)
例如,我要使用这两个矩阵(或者oo是向量,因为它是一列)
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)
输出
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
但这太慢了.
推荐答案
R>oop*drop(oo)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 8 10 12
[3,] 21 24 27
这篇关于R将矩阵的行乘以向量的行的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文