考虑2x2块的2x2矩阵,例如,
using LinearAlgebra
using StaticArrays
M = zeros(SMatrix{2, 2, Float64, 4}, 2, 2)
M[1,1] = SA[1 0; 0 2]
M[2,2] = SA[0 3; 4 0]将M转换为4x4标量矩阵的最佳方法是什么?
# how to convert M into this?
M2 = [1 0 0 0;
0 2 0 0;
0 0 0 3;
0 0 4 0]在我的实际问题中,矩阵的大小会更大,但一般的问题仍然是:如何将块矩阵平坦成一个更大的矩阵。
发布于 2021-12-20 02:15:57
我建议使用BlockArrays.jl
julia> using BlockArrays
julia> mortar(M)
2×2-blocked 4×4 BlockMatrix{Float64, Matrix{SMatrix{2, 2, Float64, 4}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}:
1.0 0.0 │ 0.0 0.0
0.0 2.0 │ 0.0 0.0
──────────┼──────────
0.0 0.0 │ 0.0 3.0
0.0 0.0 │ 4.0 0.0当然,您可以做Matrix(mortar(M))以回到“正常”矩阵。但是,如果您有这样的数据结构,您应该喜欢使用BlockArray。
https://stackoverflow.com/questions/70416660
复制相似问题