Main Content

blkdiag

Block diagonal matrix

Description

example

B = blkdiag(A1,...,AN)returns theblock diagonal matrixcreated by aligning the input matricesA1,...,ANalong the diagonal ofB.

Examples

collapse all

Create a block diagonal matrix from three matrices of different sizes.

A1 = ones(2,2); A2 = 2*ones(3,2); A3 = 3*ones(2,3); B = blkdiag(A1,A2,A3)
B =7×71 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 2 2 0 0 0 0 0 2 2 0 0 0 0 0 2 2 0 0 0 0 0 0 0 3 3 3 0 0 0 0 3 3 3

Input Arguments

collapse all

Input matrices, specified as a comma-separated list of matrices. The matrices can be either square or rectangular and can differ in size.

If any of the input matrices are sparse, then the output block diagonal matrix is also sparse.

More About

collapse all

Block Diagonal Matrix

A block diagonal matrix is a matrix whose diagonal contains blocks of smaller matrices, in contrast to a regular diagonal matrix with single elements along the diagonal. A block diagonal matrix takes on the following form, whereA1,A2,…,ANare each matrices that can differ in size:

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a

See Also