Learn R Programming

Matrix (version 0.999375-42)

bdiag: Construct a Block Diagonal Matrix

Description

Build a block diagonal matrix given several building block matrices.

Usage

bdiag(...)

Arguments

...
individual matrices or a list of matrices.

Value

  • A sparse matrix inheriting from class CsparseMatrix obtained by combining the arguments into a block diagonal matrix.

See Also

Diagonal for constructing matrices of class diagonalMatrix, or kronecker which also works for "Matrix" inheriting matrices.

bandSparse constructs a banded sparse matrix from its non-zero sub-/super - diagonals.

Note that other CRAN Rpackages have own versions of bdiag() which return traditional matrices.

Examples

Run this code
bdiag(matrix(1:4, 2), diag(3))
## combine "Matrix" class and traditional matrices:
bdiag(Diagonal(2), matrix(1:3, 3,4), diag(3:2))

mlist <- list(1, 2:3, diag(x=5:3), 27, cbind(1,3:6), 100:101)
bdiag(mlist)

stopifnot(identical(bdiag(mlist),                    bdiag(lapply(mlist, as.matrix))))

Run the code above in your browser using DataLab