3
votes

Reading related questions, I found that one can initialize in julia an arbitrary array as

B = Array{Complex{Float64}}(undef, 0, 0)

I want to initialize something similar, but I want to put sparse matrices in such array. This last construction does not work in this case.

How can I build an arbitrary array that contains sparse ones?

Actually my problem is a little bit more specific. In each entry of B I want to put a different sparse matrix. The sparse matrices are of fixed dimensions and I know beforehand how many sparse matrices I want to put into B.

1

1 Answers

3
votes
B = [spzeros(2,2) for i in 1:2, j in 1:3]

Here is what you will get:

julia> B = [spzeros(2,2) for i in 1:2, j in 1:3]
2×3 Array{SparseMatrixCSC{Float64,Int64},2}:
 2×2 SparseMatrixCSC{Float64,Int64} with 0 stored entries  2×2 SparseMatrixCSC{Float64,Int64} with 0 stored entries  2×2 SparseMatrixCSC{Float64,Int64} with 0 stored entries
 2×2 SparseMatrixCSC{Float64,Int64} with 0 stored entries  2×2 SparseMatrixCSC{Float64,Int64} with 0 stored entries  2×2 SparseMatrixCSC{Float64,Int64} with 0 stored entries

Please note that you cannot use fill for that because all elements of B would reference the same sparse array.