bdiag {Matrix} | R Documentation

## Construct a Block Diagonal Matrix

### Description

Build a block diagonal matrix given several building block matrices.

### Usage

    bdiag(...)
    .bdiag(lst)

### Arguments

`...` | individual matrices or a `list` of matrices.
`lst` | non-empty `list` of matrices.

### Details

For non-trivial argument list, `bdiag()` calls `.bdiag()`. The latter maybe useful to programmers

### Value

A sparse matrix obtained by combining the arguments into a block diagonal matrix.

The value of `bdiag()` inheris from class `CsparseMatrix`, whereas `.bdiag()` returns a `TsparseMatrix`.

### Author(s)

Martin Maechler, built on a version posted by Berton Gunter to R-help; earlier versions have been posted by other authors, notably Scott Chasalow to S-news. Doug Bates's faster implementation builds on `TsparseMatrix` objects.

### 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 R packages have own versions of `bdiag()` which return traditional matrices.

### Examples

    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))))

    ml <- c(as(matrix((1:24)%% 11 == 0, 6,4),"nMatrix"),
            rep(list(Diagonal(2, x=TRUE)), 3))
    mln <- c(ml, Diagonal(x = 1:3))
    stopifnot(is(bdiag(ml), "lsparseMatrix"),
              is(bdiag(mln),"dsparseMatrix") )

    ## random (diagonal-)block-triangular matrices:
    rblockTri <- function(nb, max.ni, lambda = 3) {
       .bdiag(replicate(nb, {
             n <- sample.int(max.ni, 1)
             tril(Matrix(rpois(n*n, lambda=lambda), n,n)) }))
    }

    (T4 <- rblockTri(4, 10, lambda = 1))
    image(T1 <- rblockTri(12, 20))

[Package Matrix version 1.2-6 Index]