# Re: [R] how can I do loop in array?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon 15 May 2006 - 05:12:41 EST

Here are two ways:

# test data
A <- diag(4)+2
B <- matrix(1:8, 4)

# using for
out <- B
for(i in 1:100) out <- A %*% out
out

junk <- replicate(100, out <<- A %*% out) out

Or you could create A2 = A %*% A and
A4 = A2 %*% A2 etc.and get A128, say,
in 7 iterations.

Another possibility if A were square symmetric is that you could raise its eigenvalues to the 100th power.

On 5/14/06, SU SU <su.113@osu.edu> wrote:
> I have two matrix: matrix A is 18 by 18 and Matrix B is 18 by 2, I want to multiply them and get new matrix. Then matrix A will multiply the new matrix again, I want to do it 100 times. Can I do it by making matrix B as an array with 18 by 2 by 100 dimension? I can not figure out how to do the loop? using apply?
> Thanks a lot!
> Su Su
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help