I have a matrix A
and I want to build a block diagonal matrix M
as
A 0 ⋯ 0
0 A ⋯ 0
⋮ ⋮ ⋱ ⋮
0 0 ⋯ A
where the 0
s are matrices of the same size as A
filled with zeros. Is there a convenient way to do that?
In other words, is there an equivalent to Python's linalg.block_diag
(from scipy)?
You can use blockdiag
from the SparseArrays.jl standard library. For example:
julia> using SparseArrays
julia> A = sparse(rand(3,2)) # A must be sparse for blockdiag
3×2 SparseMatrixCSC{Float64, Int64} with 6 stored entries:
0.465226 0.473656
0.230678 0.391924
0.928409 0.772551
julia> M = blockdiag(A, A, A)
9×6 SparseMatrixCSC{Float64, Int64} with 18 stored entries:
0.465226 0.473656 ⋅ ⋅ ⋅ ⋅
0.230678 0.391924 ⋅ ⋅ ⋅ ⋅
0.928409 0.772551 ⋅ ⋅ ⋅ ⋅
⋅ ⋅ 0.465226 0.473656 ⋅ ⋅
⋅ ⋅ 0.230678 0.391924 ⋅ ⋅
⋅ ⋅ 0.928409 0.772551 ⋅ ⋅
⋅ ⋅ ⋅ ⋅ 0.465226 0.473656
⋅ ⋅ ⋅ ⋅ 0.230678 0.391924
⋅ ⋅ ⋅ ⋅ 0.928409 0.772551
And if you really need M
to be dense, you can just convert it back with
julia> Matrix(M)
9×6 Matrix{Float64}:
0.465226 0.473656 0.0 0.0 0.0 0.0
0.230678 0.391924 0.0 0.0 0.0 0.0
0.928409 0.772551 0.0 0.0 0.0 0.0
0.0 0.0 0.465226 0.473656 0.0 0.0
0.0 0.0 0.230678 0.391924 0.0 0.0
0.0 0.0 0.928409 0.772551 0.0 0.0
0.0 0.0 0.0 0.0 0.465226 0.473656
0.0 0.0 0.0 0.0 0.230678 0.391924
0.0 0.0 0.0 0.0 0.928409 0.772551
You can do this with cat
, giving it two dimensions:
julia> A = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> cat(A, A', reverse(A); dims=(1,2))
6×6 Matrix{Int64}:
1 2 0 0 0 0
3 4 0 0 0 0
0 0 1 3 0 0
0 0 2 4 0 0
0 0 0 0 4 3
0 0 0 0 2 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With