[R] shifting a huge matrix left or right efficiently ?

Huang-Wen Chen chenh at seas.upenn.edu
Mon Oct 9 23:21:48 CEST 2006


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



More information about the R-help mailing list