您也可以尝试:(如果尺寸相同)
indx <- rep(seq(dim(x)[1]), each=dim(x)[1])
res <- apply(x,1, `*`,y) ##assuming that this is okay
res1 <- do.call(`cbind`,by(res, indx, FUN=colSums))
dimnames(res1) <- NULL
res1
# [,1] [,2]
#[1,] 23 31
#[2,] 34 46
或者
t(colSums(array(apply(x,1, `*`, y), c(dim(x),dim(x)[1]))))
# [,1] [,2]
#[1,] 23 31
#[2,] 34 46
更新
x <- matrix(seq_len(9), 3,3)
y <- matrix(seq_len(12), 3,4)
dim1 <- do.call(`pmax`, list(dim(x), dim(y)))
t(colSums(array(apply(x,1, `*`, y), c(dim1,dim(y)[1]))))
# [,1] [,2] [,3] [,4]
#[1,] 30 66 102 138
#[2,] 36 81 126 171
#[3,] 42 96 150 204