Re: [Rd] matrix bands

From: Jeremy David Silver <>
Date: Fri, 26 Aug 2011 15:08:03 +0200

Thanks for the suggestion, Martin!

I looked at both Matrix::band and Matrix::bandSparse. Maybe I misunderstand the help pages and the examples, but from what I can see neither of them provides the functionality I was looking for.

For the getter version of the function I was looking for, I can't use Matrix::band (by extracting the non-zero elements) because this would require the assumption that the specified band is entirely non-zero, e.g.:

band3 <- function(x,n){

   x <- Matrix::band(x,n,n)
   x[x!=0] ## works for a dense matrix, but not if band n has zeroes

As for the setter version of the function I was looking for, it should set the values of a specified band in an existing matrix to be the vector of values provided. As far as I understand, Matrix::bandSparse constructs a matrix rather than modifying the values of an existing matrix.

The functions included in my last post filled this gap. If Matrix::band and Matrix::bandSparse can set/get bands in this way, I would like to see how this can be done. If not, then perhaps they could be extended to achieve this functionality.

On 2011-08-26 14:08, Martin Maechler wrote:
>>>>>> Jeremy David Silver<>
>>>>>> on Fri, 26 Aug 2011 13:23:43 +0200 writes:
> > Dear R developers, I was looking for a function analogous
> > to base::diag() for getting and setting bands of a
> > matrix. The closest I could find was Matrix::band(), but
> > this was not exactly what I wanted for two
> > reasons. Firstly, Matrix::band() returns a matrix rather
> > than just the specified band. Secondly, Matrix::band()
> > cannot be used for setting the values for a matrix band.
> Yes, but did you not look at help(band)
> or not look carefully enough ?
> --->
> See Also:
> ‘bandSparse’ for the _construction_ of a banded sparse matrix
> directly from its non-zero diagonals.
> > Setting or getting a matrix band could be of interest for
> > sufficiently many users that you might consider including
> > it in base R. Alternatively, something like this could be
> > included in the Matrix package.
> well, see above and let us know if you see anything lacking in
> bandSparse().
> Till now we haven't got much feedback about it, and there may
> well be room for improvement.
> Martin Maechler, ETH Zurich and co- maintainer("Matrix")
> mailing list Received on Fri 26 Aug 2011 - 13:09:56 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

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 Fri 26 Aug 2011 - 19:30:23 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive