[R] How do I write a sum of matrixes??

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Tue May 6 09:15:45 CEST 2008


> I am a new user and I have been struggling for hours. So finally I 
> decide to ask you:
> If I have a matrix P, and P.2 = P%*%P, and P.3=P.2%*%P
> is there a function to calculate the power of a matrix?? if not how can 
i do:
> for (i in 1:10)  {P.i=P^i} 
> after this I need to sum them up and my problem is to combine P and 
> i to P.i  can anyone help me please???

Take a look at FAQ on R 7.21 ("How can I turn a string into a variable?").

This code will do what you ask (though the first matrix is named P.1, 
rather than P for convenience):

paste0 <- function(x) paste("P.", x, sep="")
P.1 <- matrix(rnorm(25), nrow=5)
varnames <- paste0(1:10)
for(i in 2:10) assign(paste0(i), get(paste0(i-1)) %*% P.1)

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list