Return the matrix obtained by setting to zero elements below a diagonal (triu), above a diagonal (tril), or outside of a general band (band).
methods
band(x, k1, k2,...)triu(x, k =0L,...)tril(x, k =0L,...)
Arguments
x: a matrix-like object
k,k1,k2: integers specifying the diagonals that are not set to zero, k1 <= k2. These are interpreted relative to the main diagonal, which is k = 0. Positive and negative values of k indicate diagonals above and below the main diagonal, respectively.
...: optional arguments passed to methods, currently unused by package Matrix.
Details
triu(x, k) is equivalent to band(x, k, dim(x)[2]). Similarly, tril(x, k) is equivalent to band(x, -dim(x)[1], k).
Returns
An object of a suitable matrix class, inheriting from triangularMatrix where appropriate. It inherits from sparseMatrix if and only if x does.
Methods
x = "CsparseMatrix": method for compressed, sparse, column-oriented matrices.
x = "RsparseMatrix": method for compressed, sparse, row-oriented matrices.
x = "TsparseMatrix": method for sparse matrices in triplet format.
x = "diagonalMatrix": method for diagonal matrices.
x = "denseMatrix": method for dense matrices in packed or unpacked format.
x = "matrix": method for traditional matrices of implicit class matrix.
See Also
bandSparse for the construction of a banded sparse matrix directly from its non-zero diagonals.
Examples
## A random sparse matrix :set.seed(7)m <- matrix(0,5,5)m[sample(length(m), size =14)]<- rep(1:9, length=14)(mm <- as(m,"CsparseMatrix"))tril(mm)# lower triangletril(mm,-1)# strict lower triangletriu(mm,1)# strict upper triangleband(mm,-1,2)# general band(m5 <- Matrix(rnorm(25), ncol =5))tril(m5)# lower triangletril(m5,-1)# strict lower triangletriu(m5,1)# strict upper triangleband(m5,-1,2)# general band(m65 <- Matrix(rnorm(30), ncol =5))# not squaretriu(m65)# result not "dtrMatrix" unless square(sm5 <- crossprod(m65))# symmetric band(sm5,-1,1)# "dsyMatrix": symmetric band preserves symmetry propertyas(band(sm5,-1,1),"sparseMatrix")# often preferable(sm <- round(crossprod(triu(mm/2))))# sparse symmetric ("dsC*")band(sm,-1,1)# remains "dsC", *however*band(sm,-2,1)# -> "dgC"