# Why do I get a sparse matrix?

Hello,

I have these two matrices:

``````julia> L
1×1 Array{Float64,2}:
3.0

julia> M
2×1 Array{Float64,2}:
2.0
1.0
``````

I do:

``````import LinearAlgebra
d = 3
Ctilde = hcat(vcat(L,M), vcat(zeros(size(L,1),d-size(L,1)), LinearAlgebra.Diagonal(ones(d-size(L,1)))))
``````

Then my matrix `Ctilde` is a sparse matrix:

``````julia> Ctilde
3×3 SparseArrays.SparseMatrixCSC{Float64,Int64} with 5 stored entries:
[1, 1]  =  3.0
[2, 1]  =  2.0
[3, 1]  =  1.0
[2, 2]  =  1.0
[3, 3]  =  1.0
``````

Why? How can I directly get an ordinary matrix? (by “directly”, I mean without getting this sparse matrix and then converting it).

`Diagonal` is a kind of sparse matrix. You can write `collect(Diagonal(...))` to get a normal matrix, or better just use `diagm`.

1 Like