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

From: Charles C. Berry <cberry_at_tajo.ucsd.edu>
Date: Mon 09 Oct 2006 - 23:43:47 GMT

A little algebra up front can often help.

Note that

         X <- shiftMatrixL(X, 1)*3 + shiftMatrixL(X,2)*5

(and similarly with more terms on the r.h.s)

is just

         X <- X %*% mat

where mat is is a matrix of zeroes except that

 	mat[ i+1, i ] == 3
 	mat[ i+2, i ] == 5

and dim(mat) == dim(X).

So forget about explicitly shifting the matrix if you do this in native R - just construct a suitable version of mat and use '%*%'. If you must do

this in C shift coefficient vector implicitly using a pointer before finding the inner product with each row, and if the matrix is truly large follow Tony Plate's advice to transform X first (and look at Chapter 1 of Matrix Computations by Golub and Van Loan, 1996, if you need to know why).

On Mon, 9 Oct 2006, 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.
>

Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry@tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0717

______________________________________________
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. Received on Tue Oct 10 09:46:53 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 10 Oct 2006 - 00:30:10 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.