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

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Tue, 06 May 2008 12:55:58 +0000 (UTC)

pascal vrolijk <pascalvrolijk_at_hotmail.com> wrote in news:BAY120-W47837F2A708683682EB44AACD60_at_phx.gbl:

> Hello best helpers,
>
> 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}

Three methods show up in a search "r-help power of a matrix":

  1. Use a pre-built function: library(msm) ?MatrixExp

2)
<https://stat.ethz.ch/pipermail/r-help/2007-May/131330.html>

3) Bill Venables offered this about a week ago in this list:



This is probably as good a way as any way for this kind of problem. First define a binary operator:

> "%^%" <- function(x, n)

        with(eigen(x), vectors %*% (values^n * t(vectors)))

Your toy example then becomes

> m <- matrix(c(1, 0.4, 0.4, 1), nrow = 2)
> m

     [,1] [,2]
[1,] 1.0 0.4
[2,] 0.4 1.0

> m %^% (-0.5)

           [,1] [,2]
[1,] 1.0680744 -0.2229201
[2,] -0.2229201 1.0680744


-- 
David Winsemius



> 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???
>
> Thanks and have a nice day,
>
> Pascal.
______________________________________________ R-help_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Received on Tue 06 May 2008 - 13:00:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 06 May 2008 - 19:30:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive