Volker
1
Hi,
can somebody tell me how to initialize a dictionary with empty arrays of type Array{Float64, 2}?
1. Alternative
dict_data = [Dict(name => Array{Float64, 2} for name in [:x1, :x2, :y1, :y2]) for i in 1:10]
dict_data[1][:x1] = zeros(10,4)
2. Alternative
dict_data = [Dict(name => Matrix{Float64} for name in [:x1, :x2, :y1, :y2]) for i in 1:10]
dict_data[1][:x1] = zeros(10,4)
But both lead to the same error message.
ERROR: MethodError: Cannot convert
an object of type Array{Float64,2} to an object of type DataType
Best regards
You don’t initialise the arrays, instead you create a dict where all entries contain Array{Float64, 2}
, which is a DataType
.
Try this:
d = Dict(name => Matrix{Float64}(undef, 0, 0) for name in [:x1, :x2, :y1, :y2])
or, for an array of dicts of matrices, as in your example:
dict_data = [copy(d) for i in 1:10]
d[1][:x1] = zeros(10, 4)
1 Like
Volker
3
Thanks. Can you explain me or send me a link, where (undef, 0, 0)
of the command Matrix{Float64}(undef, 0, 0)
is explained?
In the REPL, see
help?> Matrix
(…)
Matrix{T}(undef, m, n)
Construct an uninitialized Matrix{T} of size m×n. See undef.
Also: Multi-dimensional Arrays · The Julia Language
3 Likes
As a side note, it’s slightly more convenient to create an empty Float64 matrix with zeros(0, 0)
.
2 Likes
it’s also slightly slower since it has to write zeros, but this usually doesn’t matter.
There are not that many zeros to write in a 0x0 matrix, but yes, it’s slower anyway so I guess it takes a different code path.
1 Like