Returns a new matrix formed by extracting the lower triangle
tril) or the upper triangle (
triu) or a general band
relative to the diagonal (
band), and setting other elements
to zero. The general forms of these functions include integer
arguments to specify how many diagonal bands above or below the main
diagonal are not set to zero.
1 2 3
a matrix-like object
integers specifying the diagonal bands that will not
be set to zero. These are given relative to the main diagonal,
Optional arguments used by specific methods. (None used at present.)
An object of an appropriate matrix class. The class of the value of
triu inherits from
triangularMatrix when appropriate. Note that the
result is of class
sparseMatrix only if
method for compressed, sparse, column-oriented matrices.
method for sparse matrices in triplet format.
method for compressed, sparse, row-oriented matrices.
method for dense numeric matrices, including packed numeric matrices.
bandSparse for the construction of a banded
sparse matrix directly from its non-zero diagonals.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 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 triangle tril(mm, -1) # strict lower triangle triu(mm, 1) # strict upper triangle band(mm, -1, 2) # general band (m5 <- Matrix(rnorm(25), nc = 5)) tril(m5) # lower triangle tril(m5, -1) # strict lower triangle triu(m5, 1) # strict upper triangle band(m5, -1, 2) # general band (m65 <- Matrix(rnorm(30), nc = 5)) # not square triu(m65) # result in not dtrMatrix unless square (sm5 <- crossprod(m65)) # symmetric band(sm5, -1, 1)# symmetric band preserves symmetry property as(band(sm5, -1, 1), "sparseMatrix")# often preferable
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.