# How can I fill an array with empty 2D arrays?

**URL:** <https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895>\
**Category:** New to Julia\
**Created:** [May 10, 2021, 3:02pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895 "2021-05-10T15:02:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamazalotta](https://avatars.discourse-cdn.com/v4/letter/z/b77776/32.png) [@zamazalotta](https://discourse.julialang.org/u/zamazalotta)\
**Post date:** [May 10, 2021, 3:02pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/1 "2021-05-10T15:02:59Z")

</div>

Hello,  
I am going into a loop which will produce N 2D arrays. I want to store them in an array, so I want to define it beforehand. I know the syntax for filling with empty 1D arrays but I can’t figure our the 2D version. Can you help me out?  
Thanks

1D version that works  
`mat3Dtt = fill(Float64[],1, N);`

2D version I can’t get to work

```julia
mat3Dtt = fill(Array{Float64,2},1, N);
julia> mat3Dtt[1]=rand(3,3)
ERROR: MethodError: Cannot `convert` an object of type Array{Float64,2} to an object of type DataType

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 10, 2021, 3:13pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/3 "2021-05-10T15:13:41Z")

</div>

You can do this, but be warned that `fill` repeats the same element, it doesn’t make copies.

```julia
julia> x = fill(Matrix{Float64}(undef,0,0), 3);

julia> x[1] = reshape(1:6,2,3);

julia> x # ok!
3-element Vector{Matrix{Float64}}:
 [1.0 3.0 5.0; 2.0 4.0 6.0]
 0×0 Matrix{Float64}
 0×0 Matrix{Float64}

julia> y = fill(Matrix{Float64}(undef,2,3), 3);

julia> y[1] === y[2]
true

julia> y[1] .= reshape(1:6,2,3);

julia> y
3-element Vector{Matrix{Float64}}:
 [1.0 3.0 5.0; 2.0 4.0 6.0]
 [1.0 3.0 5.0; 2.0 4.0 6.0]
 [1.0 3.0 5.0; 2.0 4.0 6.0]

```

But you may be better off just letting a comprehension, or `map`, create the output for you. Or doing `z = Matrix{Float64}[]; push!(z, reshape(1:6,2,3))`.

---

<div class="post-metadata">

**Author:** ![zamazalotta](https://avatars.discourse-cdn.com/v4/letter/z/b77776/32.png) [@zamazalotta](https://discourse.julialang.org/u/zamazalotta)\
**Post date:** [May 10, 2021, 3:17pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/4 "2021-05-10T15:17:01Z")

</div>

> [@mcabbott](#):
>
> `x = fill(Matrix{Float64}(undef,0,0), 3);`

i will go with that, thank you very much!

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [May 10, 2021, 3:45pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/5 "2021-05-10T15:45:00Z")

</div>

If you’re replacing the entire array that’s fine, but if you intend to predefine and then fill them, it’s still a problem:

```julia
x = fill(Matrix{Float64}(undef,2,2), 3);

julia> x
3-element Vector{Matrix{Float64}}:
 [2.75859452845e-313 3.3951932658e-313; 2.33419537065e-313 6.94677762088306e-310]
 [2.75859452845e-313 3.3951932658e-313; 2.33419537065e-313 6.94677762088306e-310]
 [2.75859452845e-313 3.3951932658e-313; 2.33419537065e-313 6.94677762088306e-310]

julia> x[1][1,1] = 5
5

julia> x
3-element Vector{Matrix{Float64}}:
 [5.0 3.3951932658e-313; 2.33419537065e-313 6.94677762088306e-310]
 [5.0 3.3951932658e-313; 2.33419537065e-313 6.94677762088306e-310]
 [5.0 3.3951932658e-313; 2.33419537065e-313 6.94677762088306e-310]

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 10, 2021, 3:45pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/6 "2021-05-10T15:45:57Z")

</div>

> [@zamazalotta](#):
>
> i will go with that, thank you very much!

Make sure you understand @mcabbott’s warning:

> [@mcabbott](#):
>
> You can do this, but be warned that `fill` repeats the same element, it doesn’t make copies.

That means you will get an array consisting of the _same_ matrix 3 times. Modifying any one of those will modify all of them because they are all the same value. This is almost certainly not what you want in any application.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 10, 2021, 3:46pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/7 "2021-05-10T15:46:29Z")

</div>

> [@BioTurboNick](#):
>
> If you’re replacing the entire array that’s fine,

Yeah, although if you’re doing that then pre-allocating the individual arrays is pointless, since they’ll be replaced regardless.

---

<div class="post-metadata">

**Author:** ![zamazalotta](https://avatars.discourse-cdn.com/v4/letter/z/b77776/32.png) [@zamazalotta](https://discourse.julialang.org/u/zamazalotta)\
**Post date:** [May 10, 2021, 5:57pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/8 "2021-05-10T17:57:05Z")

</div>

Yes, I am replacing the entire array, using it a bit like this:

```julia
julia> a=fill(Array{Float32,2}(undef,0,0),1,3);

julia> a[1]=rand(2,2); a[2]=rand(3,3);

julia> a[:]
3-element Array{Array{Float32,2},1}:
 [0.92491776 0.93883944; 0.9667731 0.9894779]
 [0.13091879 0.37846377 0.07404064; 0.71520746 0.9362733 0.41508183; 0.8309387 0.73587126 0.0631132]      
 Array{Float32}(undef,0,0)

```

I understand that I could just push the new one as it is generated, however I think pre-allocating makes the code more readable as you know where each array is going to go beforehand, instead of counting how many pushes there are.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 10, 2021, 6:06pm UTC](https://discourse.julialang.org/t/how-can-i-fill-an-array-with-empty-2d-arrays/60895/9 "2021-05-10T18:06:26Z")

</div>

I see. Then perhaps what you actually need is:

```julia
a = Array{Array{Float32, 2}, 1}(undef, 3)

```

which is a `Vector` (a 1-D `Array`) whose elements are themselves matrices, with space allocated to hold 3 elements.

You can write that more simply as: `Vector{Matrix{Float32}}(undef, 3)` if you prefer.
