# Initialization of array of arrays with \`fill(ones(1),2,2)\`: only one vector is created

**URL:** <https://discourse.julialang.org/t/initialization-of-array-of-arrays-with-fill-ones-1-2-2-only-one-vector-is-created/48048>\
**Category:** New to Julia\
**Created:** [October 9, 2020, 4:14pm UTC](https://discourse.julialang.org/t/initialization-of-array-of-arrays-with-fill-ones-1-2-2-only-one-vector-is-created/48048 "2020-10-09T16:14:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 9, 2020, 4:14pm UTC](https://discourse.julialang.org/t/initialization-of-array-of-arrays-with-fill-ones-1-2-2-only-one-vector-is-created/48048/1 "2020-10-09T16:14:21Z")

</div>

Disclaimer: this is explicit in the help entry of fill, so I am not complaining of anything…

```julia
julia> m = fill(ones(1),2,2)
2×2 Array{Array{Float64,1},2}:
 [1.0] [1.0]
 [1.0] [1.0]

julia> m[1,1][1] = 2
2

julia> m
2×2 Array{Array{Float64,1},2}:
 [2.0] [2.0]
 [2.0] [2.0]

```

Clearly `ones(1)` was called only once and the vectors at all positions are the same.

Using `fill(copy(ones(1)),2,2)` (this was more strange to me) also results in the same thing.

Is there, therefore, a simple syntax to use `fill` to create an array of independent arrays?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 9, 2020, 4:21pm UTC](https://discourse.julialang.org/t/initialization-of-array-of-arrays-with-fill-ones-1-2-2-only-one-vector-is-created/48048/2 "2020-10-09T16:21:39Z")

</div>

No you can’t. You can use a comprehension instead, sth like `[ones(1) for i in 1:2, j in 1:2]`.

---

<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:** [October 9, 2020, 4:24pm UTC](https://discourse.julialang.org/t/initialization-of-array-of-arrays-with-fill-ones-1-2-2-only-one-vector-is-created/48048/3 "2020-10-09T16:24:13Z")

</div>

> [@lmiq](#):
>
> Is there, therefore, a simple syntax to use `fill` to create an array of independent arrays?

Not really. After calling `fill`, it should always be the case that `m[i] === m[j]` because `fill` fills the array with the _same_ (identical in every meaningful way in Julia) value.

A list comprehension like:

```julia
[ones(1) for _ in i:2, _ in 1:2]

```

is a common way to accomplish what you’re looking for. Alternatively, you can create a new array of arrays with `Matrix{Vector{Float64}}(undef, 2, 2)` and then fill it in a loop. Either solution should perform very well.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 9, 2020, 4:24pm UTC](https://discourse.julialang.org/t/initialization-of-array-of-arrays-with-fill-ones-1-2-2-only-one-vector-is-created/48048/4 "2020-10-09T16:24:58Z")

</div>

Great, perfect. I was trying to figure out which were the alternatives.
