我会非常简短!这是一个可重复的例子:
Z1 <- matrix(rep(c(1, 0, 0, 0, 0,
1, 1, 0, 0, 0,
1, 0, 1, 0, 0,
1, 0, 0, 1, 0,
1, 0, 0, 0, 1),
times = 3),
nrow = 15,
ncol = 5,
byrow = TRUE)
z <- matrix(c(0.1, 0.2, 0.3, 0.4, 0.5,
0.6, 0.7, 0.8, 0.9, 1,
1, 2, 3, 4, 5),
nrow = 3,
ncol = 5,
byrow = TRUE)
我需要一个可扩展的非for
- 循环解决方案(例如,涉及apply
- 系列中的某些内容),它为这些计算提供了相同的结果,我现在将手工执行:
Zz1 <- Z1[1:5,] %*% z[1,]
Zz2 <- Z1[6:10,] %*% z[2,]
Zz3 <- Z1[11:15,] %*% z[3,]
Zz <- rbind(Zz1, Zz2, Zz3)
答案 0 :(得分:5)
非常类似于question from earlier today:
rowSums(Z1 * z[rep(1:3,each=5),])
#[1] 0.1 0.3 0.4 0.5 0.6 0.6 1.3 1.4 1.5 1.6 1.0 3.0 4.0 5.0 6.0