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

From: Gabor Grothendieck <>
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

# using replicate, see ?replicate for more info out <- B
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 <> 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
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Mon May 15 05:17:25 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 15 May 2006 - 06:10:07 EST.

Mailing list information is available at Please read the posting guide before posting to the list.