문제

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.

도움이 되었습니까?

해결책

R>oop*drop(oo)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    8   10   12
[3,]   21   24   27
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top