Reputation: 339
x is the reference matrix for all the iterations. there are total 2 matrices.
matrix A
1 4 1 4
4 2 4 2
2 3 2 3
3 3 3 3
matrix B
1 4 1 4
4 2 4 2
2 3 2 3
3 3 3 3
matrix x
4 1 4 3
2 4 2 2
3 2 3 5
3 5 1 1
Here matrix A and B is same. We need a matrix multiplication in the below fasion:
C <- B %*% x
D <- C %*% x
E <- D %*% x
F <- E %*% x
x is the reference matrix for all the iterations. this multiplication is done for 15 times so need help in writting in a loop or function.
Upvotes: 1
Views: 783
Reputation: 25425
If you only want the last matrix we can use a recursive function, as suggested by Sotos:
A <- matrix(runif(9), 3)
X <- matrix(runif(9), 3)
repmult <- function(A,x,reps)
{
if(reps==0){
return(A)
}
else
{
repmult(A%*%x,x,reps-1)
}
}
repmult(A,X,15)
If you want all intermediate results in a list as well, we can modify the function from the answer on this SO question (although you may want to change its name):
Mpow <- function(A,x, n) {
L <- list(A%*%x)
if (n==1) return(L)
P <- A
for (i in 1:n) L[[i]] <- (P <- P %*% x)
return(L)
}
Mpow(A,X,3)
Upvotes: 2