# How to initialize a dictionary with empty arrays of type Array{Float64, 2}?

**URL:** <https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 10, 2019, 7:35am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085 "2019-12-10T07:35:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [December 10, 2019, 7:35am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085/1 "2019-12-10T07:35:22Z")

</div>

Hi,

can somebody tell me how to initialize a dictionary with empty arrays of type Array{Float64, 2}?

**1. Alternative**

```julia
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**

```julia
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

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [December 10, 2019, 8:11am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085/2 "2019-12-10T08:11:52Z")

</div>

You don’t initialise the arrays, instead you create a dict where all entries contain `Array{Float64, 2}`, which is a `DataType`.  
Try this:

```julia
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:

```julia
dict_data = [copy(d) for i in 1:10]
d[1][:x1] = zeros(10, 4)

```

---

<div class="post-metadata">

**Author:** ![Volker](https://avatars.discourse-cdn.com/v4/letter/v/77aa72/32.png) [@Volker](https://discourse.julialang.org/u/Volker)\
**Post date:** [December 10, 2019, 8:50am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085/3 "2019-12-10T08:50:54Z")

</div>

Thanks. Can you explain me or send me a link, where `(undef, 0, 0)`  
of the command `Matrix{Float64}(undef, 0, 0)` is explained?

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [December 10, 2019, 8:59am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085/4 "2019-12-10T08:59:20Z")

</div>

In the REPL, see

```julia
help?> Matrix

```

(…)

```julia
  Matrix{T}(undef, m, n)

  Construct an uninitialized Matrix{T} of size m×n. See undef.

```

Also: [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Construction-and-Initialization-1)

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 10, 2019, 10:43am UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085/5 "2019-12-10T10:43:05Z")

</div>

As a side note, it’s slightly more convenient to create an empty Float64 matrix with `zeros(0, 0)`.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 10, 2019, 2:56pm UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085/6 "2019-12-10T14:56:54Z")

</div>

it’s also slightly slower since it has to write zeros, but this usually doesn’t matter.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 10, 2019, 3:40pm UTC](https://discourse.julialang.org/t/how-to-initialize-a-dictionary-with-empty-arrays-of-type-array-float64-2/32085/7 "2019-12-10T15:40:57Z")

</div>

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.
