# How do I create a Matrix of Vectors?

**URL:** <https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524>\
**Category:** New to Julia\
**Created:** [December 10, 2018, 2:11pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524 "2018-12-10T14:11:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmg.duarte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmg.duarte/32/16392_2.png) [@jmg.duarte](https://discourse.julialang.org/u/jmg.duarte)\
**Post date:** [December 10, 2018, 2:11pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/1 "2018-12-10T14:11:28Z")

</div>

Hello everyone!

I’m currently doing the Advent of Code challenges (I’m not asking for help regarding solving it) and for day 4 I’m trying to use an array of vectors.

For the creation I tried `fill(Vector{Int}(), (1000, 1000))` however this copies the result of `Vector{Int}()`  
So, whenever I do `push!(arr[y, x], elem)` all positions get `elem` added to them since they’re all the same one.

Is this desired behavior? I understand it works for literal values like `3.14` or `"hello"` however for my use case the only solution I see would be

```julia
for i in arr
    arr[i] = Vector{Int}()
end

```

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 10, 2018, 2:28pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/2 "2018-12-10T14:28:42Z")

</div>

Consider something like

```julia
function array_of_empty_vectors(T, dims...)
    array = Array{Vector{T}}(undef, dims...)
    for i in eachindex(array)
        array[i] = Vector{T}()
    end
    array
end

# this creates a 2x3 matrix with independent Vector{Int}()'s
array_of_empty_vectors(Int, 2, 3)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 10, 2018, 2:30pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/3 "2018-12-10T14:30:08Z")

</div>

Also, regading

> [@jmg.duarte](#):
>
> Is this desired behavior?

Yes, `fill` does not create copies, so each element will be the same vector.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 10, 2018, 3:02pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/4 "2018-12-10T15:02:01Z")

</div>

```julia
A = fill(Vector{Int}(), (1000, 1000));
A .= copy.((Vector{Int}(),));

```

Or

```julia
A = [copy(Vector{Int}()) for i in 1:1000, j in 1:1000];

```

---

<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, 2018, 3:44pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/5 "2018-12-10T15:44:33Z")

</div>

> [@mohamed82008](#):
>
> A = [copy(Vector{Int}()) for i in 1:1000, j in 1:1000];

No need for the `copy` here and I’d say that the comprehension is the standard solution. But this is also cute:

```julia
julia> 0 .|> fill(x->Vector{Int}(), 2, 3)
2×3 Array{Array{Int64,1},2}:
 [] [] []
 [] [] []

```

Does someone know if this can be done without a dummy input variable?

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [December 10, 2018, 10:41pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/6 "2018-12-10T22:41:47Z")

</div>

Slightly shorter solution:

```julia
[Int[] for i=1:1000, j=1:1000]

```

> [@GunnarFarneback](#):
>
> Does someone know if this can be done without a dummy input variable?

You can do `(f->f()).(fill(()->Vector{Int}(),2,3))`, but it’s not that readable. And unlike the comprehension, it needlessly allocates a temporary array of functions.

---

<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 11, 2018, 7:23am UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/7 "2018-12-11T07:23:32Z")

</div>

> [@yha](#):
>
> And unlike the comprehension, it needlessly allocates a temporary array of functions.

That doesn’t necessarily make a blip in your profile though, unless your matrices are tiny.

```julia
julia> @btime fill(()->Int[], 1000, 1000);
  24.891 ns (1 allocation: 80 bytes)

```

I kinda like this construction, although there’s still a high amount of magic in it.

```julia
array_of_empty_vectors(T, dims...) = T .|> fill(T -> T[], dims...)

```

---

<div class="post-metadata">

**Author:** ![jmg.duarte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmg.duarte/32/16392_2.png) [@jmg.duarte](https://discourse.julialang.org/u/jmg.duarte)\
**Post date:** [December 11, 2018, 1:03pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/8 "2018-12-11T13:03:21Z")

</div>

Can you break down the syntax here?  
The whole beginning `0 .|>` is weird for me

---

<div class="post-metadata">

**Author:** ![jmg.duarte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmg.duarte/32/16392_2.png) [@jmg.duarte](https://discourse.julialang.org/u/jmg.duarte)\
**Post date:** [December 11, 2018, 1:03pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/9 "2018-12-11T13:03:22Z")

</div>

What does the `.=` do?  
The list comprehension seems more elegant to me, any guidelines on better practices?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 11, 2018, 1:14pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/10 "2018-12-11T13:14:39Z")

</div>

Please review the [documentation about broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Array-and-Vectorized-Operators-and-Functions-1).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 11, 2018, 1:29pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/11 "2018-12-11T13:29:23Z")

</div>

> [@GunnarFarneback](#):
>
> Does someone know if this can be done without a dummy input variable?

I think you need some shape to broadcast on. Another version could be

```julia
(_ -> Vector{Int}()).(fill(nothing, 2, 3))

```

where, if you wrap it in a function, I think the compiler is smart enough to ignore the redundant parts. Another option is

```julia
(T -> Vector{T}()).(fill(Int, 2, 3))

```

which almost borders on being readable 😉

---

<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 11, 2018, 1:46pm UTC](https://discourse.julialang.org/t/how-do-i-create-a-matrix-of-vectors/18524/12 "2018-12-11T13:46:46Z")

</div>

> [@jmg.duarte](#):
>
> Can you break down the syntax here?

In short, fill a matrix with copies of an anonymous function that for any input value produces a fresh empty vector. Then transform that matrix into a new matrix by, element for element, running that function with 0 as input. The result is a matrix of empty vectors.

> [@jmg.duarte](#):
>
> The whole beginning `0 .|>` is weird for me

Sorry about that, I had to trick the dot broadcast syntax into doing what I wanted. That’s why I regarded the comprehension as the standard solution.
