Given some diagonal matrix in Julia like A = Diagonal(rand(3,3))
, is there any way I can create a one-dimensional array whose elements are the diagonal entries of this Diagonal matrix A
?
There is diag(A, k::Integer=0)
that returns the kth diagonal of a matrix A, as a vector.
julia> A = Diagonal(rand(3,3))
3×3 Diagonal{Float64, Vector{Float64}}:
0.213159 ⋅ ⋅
⋅ 0.034186 ⋅
⋅ ⋅ 0.539693
julia> diag(A)
3-element Vector{Float64}:
0.21315894297089488
0.03418604147090787
0.5396925608269262
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