Skip to content
Prev 171177 / 398503 Next

Matrix package: band matrix

If you can tolerate the subdiagonal and superdiagonal zero elements  
being populated, then perhaps this is useful. If there is a subset  
function as you suggest, then perhaps further reduction would be  
feasible from this as a starting point. At least it would reduce the  
size from 10^5 x 10^5 to 10^5 x 3:

library(Matrix)
xxx <- data.frame(x1 =1:5, y1=c(0,6,0,7,0) )
M= Matrix(0,5,5)
M[row(M) == col(M)] <- xxx$x1
M[row(M)-1 == col(M)] <- xxx$y1[1:(length(xxx$y1)-1)]
M[row(M) == col(M)-1] <- xxx$y1[1:(length(xxx$y1)-1)]

 > M
5 x 5 sparse Matrix of class "dgCMatrix"

[1,] 1 6 . . .
[2,] 6 2 7 . .
[3,] . 7 3 8 .
[4,] . . 8 4 9
[5,] . . . 9 5