Reputation: 13
If I want to compute the first k
powers (say, k = 10
) of some matrix A
, using matrix.power
from the matrixcalc
package, would I really need to write A2 <- matrix.power(A, 2)
, A3 <- matrix.power(A, 3)
, ..., A10 <- matrix.power(A, 10)
? It seems a bit tedious to me, but I tried to avoid loops, which I understand is recommended in R, because it is not efficient. Would I be able to do some magic with apply
functions? Basically, I would need the sum of some specific entry from all the powers of the matrix A
up to a k
.
Upvotes: 1
Views: 163
Reputation: 887098
An option is lapply
library(matrixcalc)
lst1 <- lapply(2:100, matrix.power, x = A)
and then get the sum with Reduce
Reduce(`+`, lst1)
Upvotes: 1