Reputation: 183
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).
Upvotes: 2
Views: 4672
Reputation: 183
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}[ ]
Upvotes: 2
Reputation: 7893
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
Upvotes: 4