Main Content

blkdiag

Block diagonal matrix

Description

B = blkdiag(A1,...,AN) returns the block diagonal matrix created by aligning the input matrices A1,...,AN along the diagonal of B.

example

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

     1     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, where A1, A2,…, AN are each matrices that can differ in size:

Illustration of a diagonal matrix with blocks A1, A2, and so on along the main diagonal.

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