If you're able to work with the transpose of your matrix, you might 
consider the function 'filter()', e.g.:

 > filter(diag(1:5), c(2,3), sides=1)
Time Series:
Start = 1
End = 5
Frequency = 1
   [,1] [,2] [,3] [,4] [,5]
1   NA   NA   NA   NA   NA
2    3    4    0    0    0
3    0    6    6    0    0
4    0    0    9    8    0
5    0    0    0   12   10
 >

I don't know if the conversion to and from a time-series class will 
impact the timing, but if this might serve your purposes, it's easy to 
do some experiments to find out.

- Tony Plate

Huang-Wen Chen wrote:
> I'm wondering what's the best way to shift a huge matrix left or right.
> My current implementation is the following:
> 
> shiftMatrixL <- function(X, shift, padding=0) {
>   cbind(X[, -1:-shift], matrix(padding, dim(X)[1], shift))
> }
> 
> X <- shiftMatrixL(X, 1)*3 + shiftMatrixL(X,2)*5...
> 
> However, it's still slow due to heavy use of this function.
> The resulting matrix will only be read once and then discarded,
> so I believe the best implementation of this function is in C,
> manipulating the internal data structure of this matrix.
> Anyone know similar package for doing this job ?
> 
> Huang-Wen
> 
> ______________________________________________
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
R-help@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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to