[R] how can I do loop in array?

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 14 21:12:41 CEST 2006


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

# 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 <su.113 at 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 at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list