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

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Tue, 06 May 2008 14:48:20 -0400

Hi Pascal,

I think the function could be better but try this:

# Function: M is your matrix and n MUST be an integer>0 mat.pow<-function(M,n) {
  result<-M
    if(n>1){

       for ( iter in 2:n) result<-M%*%result
       result
           }

    else {result}
  result
 }

# The matrix
m <- rbind(c(1,1,0), c(0,1,1), c(0,0,1))

# Goal m^2 = m x m
goal=m%*%m

# matpow
res=mat.pow(m,2)

# Check point
all.equal(goal,res)

See RSiteSearch('nth step transition matrices')

HTH, Jorge

On Tue, May 6, 2008 at 1:37 AM, pascal vrolijk <pascalvrolijk_at_hotmail.com> wrote:

> 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}
> 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.
>
>
> _________________________________________________________________
> [[elided Hotmail spam]]
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

        [[alternative HTML version deleted]]



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 - 18:51:26 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 Wed 07 May 2008 - 13:30:34 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