blkdiag
Block diagonal matrix
Syntax
Description
B = blkdiag(
returns the block diagonal
matrix created by aligning the input matrices A1,...,AN
)A1,...,AN
along
the diagonal of B
.
Examples
Diagonal of Three Matrices
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
A1,...,AN
— Input matrices
matrices
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
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:
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™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The blkdiag
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. (since R2024b)
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)