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

Extended Capabilities

expand all

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