https://github.com/cran/pracma
Tip revision: e2512490ec9e26c0bfb230f9fd97bcf07cc8c07b authored by Hans W. Borchers on 10 November 2023, 00:10:02 UTC
version 2.4.4
version 2.4.4
Tip revision: e251249
blkdiag.R
blkdiag <-function(...) {
dots <- list(...)
if (! all(sapply(dots, is.matrix)) ||
! all(sapply(dots, is.numeric)) )
stop("All input arguments in '...' must be numeric matrices")
nrows <- sapply(dots, nrow)
ncols <- sapply(dots, ncol)
if (any(nrows == 0) || any(ncols == 0))
stop("All input matrices '...' must be non-empty.")
n <- sum(nrows)
N <- c(0, cumsum(nrows))
m <- sum(ncols)
M <- c(0, cumsum(ncols))
A <- matrix(0, nrow = n, ncol = m)
k <- length(dots)
for (i in 1:k) {
A[(N[i]+1):N[i+1], (M[i]+1):M[i+1]] <- dots[[i]]
}
return(A)
}