I have a numeric matrix
with 25 columns and 23 rows, and a vector of length 25. How can I multiply each row of the matrix by the vector without using a for
loop?
The result should be a 25x23 matrix (the same size as the input), but each row has been multiplied by the vector.
Added reproducible example from @hatmatrix's answer:
matrix <- matrix(rep(1:3,each=5),nrow=3,ncol=5,byrow=TRUE)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
vector <- 1:5
Desired output:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 2 4 6 8 10
[3,] 3 6 9 12 15
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…