Re: [R] blockwise sums

2004-09-01 Thread Vicente Canto Casasola
Hi, all!!
From the help page for 'aggregate':
Splits the data into subsets, computes summary statistics for
each, and returns the result in a convenient form.
So here's the solution I found to this problem:
blocksums <- function(x,n)
{
temp <- 1:length(x)-1
temp <- list((temp%/%n)+1)
aggregate(x,by=temp,sum)$x
}
For instance:
blocksums(1:10,3)
[1]  6 15 24 10
Hope this helps!!
Vicente.
__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] Re: matrix exponential: M0

2004-01-22 Thread Vicente Canto Casasola
H i, all!

First of all, I'd like to apologize for my poor English. It's for years 
I don't use it.

This is a R-version of a function I wrote a long ago for my HP48 calculator.
It works with the binary expression of the power and just need to 
duplicate the mem used by X.

Hope this helps.

mtx.exp<-function(X,n)
#Function to calculate the n-th power of a matrix X;
{
phi <- diag(rep(1,length(X[1,])))
pot <- X #This is the first power of the matrix.
while (n > 0)
 {
 if (n%%2)
   {
   phi <- phi%*%pot;
   }
   n <- n%/%2;
   pot <- pot %*% pot;
 }
return(phi);
}


#Here is some output:

> xx <- matrix(c(1,0,1,1),2,2)
> xx
[,1] [,2]
[1,]11
[2,]01
> mtx.exp(xx,3)
[,1] [,2]
[1,]13
[2,]01
> mtx.exp(xx,4)
[,1] [,2]
[1,]14
[2,]01
> mtx.exp(xx,6)
[,1] [,2]
[1,]16
[2,]01
> mtx.exp(xx,10)
[,1] [,2]
[1,]1   10
[2,]01
> mtx.exp(xx,1000)
[,1] [,2]
[1,]1 1000
[2,]01
Vicente D. Canto Casasola

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html