2
votes

How can I make a vector of (non-sparse) matrices in Julia? Then I want to use push! to add elements to that.

So if the name of the vector is V, then V[1] will be a matrix or Array{Float64,2}.

I know this works if the elements of the vector are sparse: V = Array(SparseMatrixCSC).

2

2 Answers

4
votes

You can use the Matrix alias (Array{T, 2}):

julia> v = Matrix{Float64}[]
0-element Array{Array{Float64,2},1}

julia> x = rand(2, 2)
2×2 Array{Float64,2}:
 0.0877254  0.256971
 0.719441   0.653947

julia> push!(v, x)
1-element Array{Array{Float64,2},1}:
 [0.0877254 0.256971; 0.719441 0.653947]

julia> v[1]
2×2 Array{Float64,2}:
 0.0877254  0.256971
 0.719441   0.653947
2
votes

I just tried this and it worked:

V = Array(Array{Float64,2}, 0);

edit: As @pkofod suggested, this way is prefered: T = Array{Float64,2}; V = Array{T}(0)

other options: V = Array{Float64,2}[ ] or V = Matrix{Float64}[ ]