R中矩阵和的计算

Calculation of the sum of matrices in R

我在思考问题。如何在 R

中计数

(A方阵,k-任意自然数) WITHOUT "for"?

如果我正确地解释了你的符号,那么在 base R 中可能是这样的...

A <- matrix(c(1,2,3,4), nrow = 2)                         #example matrix

k <- 10

B <- Reduce(`%*%`, (rep(list(A), k)), accumulate = TRUE)  #list of A^(1:k)

BB <- lapply(1:k, function(k) B[[k]]/k)                   #list of A^(1:k)/k

Reduce(`+`, BB)                                           #sum of series BB

         [,1]    [,2]
[1,] 603684.8 1319741
[2,] 879827.1 1923425