# Empty vector loop

**URL:** <https://discourse.julialang.org/t/empty-vector-loop/83898>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 7, 2022, 1:58pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898 "2022-07-07T13:58:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [July 7, 2022, 1:58pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/1 "2022-07-07T13:58:15Z")

</div>

Hi. I’m looking to code the following pseudo-loop in Julia.

```julia
x=[]; # empty vector
for t in 0:2
x=[x t] 
end

```

The answer should be x=[0 1 2]. The error I get is this.

> DimensionMismatch(“mismatch in dimension 1 (expected 0 got 1)”)
> 
> Stacktrace:  
> [1] \_cs at ./abstractarray.jl:1501 [inlined]  
> [2] \_cshp at ./abstractarray.jl:1497 [inlined]  
> [3] cat\_shape at ./abstractarray.jl:1476 [inlined] (repeats 2 times)  
> [4] \_cat\_t at ./abstractarray.jl:1521 [inlined]  
> [5] #cat\_t#110 at ./abstractarray.jl:1518 [inlined]  
> [6] \_cat at ./abstractarray.jl:1516 [inlined]  
> [7] #cat#111 at ./abstractarray.jl:1654 [inlined]  
> [8] hcat(::Array{Any,1}, ::Int64) at ./abstractarray.jl:1634  
> [9] top-level scope at ./In[559]:3  
> [10] include\_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091  
> [11] execute\_code(::String, ::String) at /Users/Name/.julia/packages/IJulia/a1SNk/src/execute\_request.jl:27  
> [12] execute\_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/Name/.julia/packages/IJulia/a1SNk/src/execute\_request.jl:86  
> [13] #invokelatest#1 at ./essentials.jl:710 [inlined]  
> [14] invokelatest at ./essentials.jl:709 [inlined]  
> [15] eventloop(::ZMQ.Socket) at /Users/Name/.julia/packages/IJulia/a1SNk/src/eventloop.jl:8  
> [16] (::IJulia.var"#15#18")() at ./task.jl:356

The problem seems to be in the creation of the empty vector, but the alternatives I have tried (zeros(0), Vector{Float64} etc.) don’t seem to work either. I am using Julia 1.5.3. Thank you.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 7, 2022, 2:02pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/2 "2022-07-07T14:02:21Z")

</div>

You want `push!` instead of what you are currently doing

```julia
x = []
for t in 0:2
    push!(x, t)
end

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 7, 2022, 5:26pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/3 "2022-07-07T17:26:40Z")

</div>

If you plan to make a vector of `Int`s, you should specify the element type:

```julia
x = Int[]

```

Otherwise, you will end up with a `Vector{Any}`, which is likely to have poor performance.

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [July 8, 2022, 10:56am UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/4 "2022-07-08T10:56:50Z")

</div>

Thanks, that seems to have worked.

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [July 8, 2022, 10:57am UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/5 "2022-07-08T10:57:23Z")

</div>

Thanks for the indication.

---

<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:** [July 8, 2022, 12:43pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/6 "2022-07-08T12:43:50Z")

</div>

Just a comment, note that the error is associated to the fact that to set a vector in Julia you need a `comma`. Because this works:

```julia
julia> x = []
       for t in 0:2
           x = [x, t] # add comma here
       end

julia> x
2-element Vector{Any}:
  Any[Any[Any[], 0], 1]
 2

```

But note that this is not what you expected, of course, because when you do `x = [x, t]` the first element will be the `vector` x, thus this will become a vector of nested empty vectors, etc.

You would need to expand the vector into its elements inside the loop, doing this:

```julia
julia> x = []
       for t in 0:2
           x = [x..., t] # the ... is called the splatting operator
       end
       x
3-element Vector{Int64}:
 0
 1
 2

```

Now you get what you expected.

None of these things is betther than `push` into a properly typed vector `Int[]`, though, because in these cases I show you are creating a new vector at every iteration of the loop, instead of just adding elements to an existing vector.

---

<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:** [July 8, 2022, 12:55pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/7 "2022-07-08T12:55:50Z")

</div>

> [@lmiq](#):
>
> `x = [x..., t] # the ... is called the splatting operator`

This works, but splatting is very inefficient when used with containers whose size isn’t known to the compiler. In fact, all you actually need is the `;` operator, which already concatenates vertically:

```julia
julia> x = [1, 2];

julia> x = [x; 3]
3-element Vector{Int64}:
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 8, 2022, 6:51pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/8 "2022-07-08T18:51:23Z")

</div>

You might as well just write `[x t]` like the OP did, since this does horizontal concatenation.

But `push!` would be better still.

---

<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:** [July 8, 2022, 7:19pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/9 "2022-07-08T19:19:41Z")

</div>

In that specific code that does not work.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 8, 2022, 10:18pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/10 "2022-07-08T22:18:05Z")

</div>

Are you saying `vcat` works, but not `hcat`? (I can’t test it on my phone.)

---

<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:** [July 8, 2022, 11:28pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/11 "2022-07-08T23:28:20Z")

</div>

It was the error OP was getting:

```julia
julia> x = []
       for i in 1:2
           x = [x i]
       end
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 0 got 1)")

```

which is this:

```julia
julia> hcat([],1)
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 0 got 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:** [July 8, 2022, 11:29pm UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/12 "2022-07-08T23:29:57Z")

</div>

(one could argue that both `hcat` and `vcat` should return the same thing, which is a vector with 1 element - not sure if there is a deep consistency reason for `hcat` not work)

---

<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:** [July 9, 2022, 7:20am UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/13 "2022-07-09T07:20:55Z")

</div>

```julia
julia> x = zeros(Int, 1, 0)
1×0 Matrix{Int64}

julia> for i in 1:2
           x = [x i]
       end

julia> x
1×2 Matrix{Int64}:
 1 2

```

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [July 9, 2022, 7:30am UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/14 "2022-07-09T07:30:05Z")

</div>

Thank you for the explanation.

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [July 9, 2022, 7:30am UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/15 "2022-07-09T07:30:53Z")

</div>

Thank you for the clarification.

---

<div class="post-metadata">

**Author:** ![AS90](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AS90](https://discourse.julialang.org/u/AS90)\
**Post date:** [July 9, 2022, 7:31am UTC](https://discourse.julialang.org/t/empty-vector-loop/83898/16 "2022-07-09T07:31:16Z")

</div>

Thanks a lot.
