# \`vcat(\[\], \[1 2\])\` # OK. \`vcat(\[\], \[1 "2"\])\` # Not OK

**URL:** <https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991>\
**Category:** General Usage\
**Tags:** question, syntax, cat\
**Created:** [April 16, 2024, 5:28am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991 "2024-04-16T05:28:55Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 16, 2024, 5:28am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/1 "2024-04-16T05:28:55Z")

</div>

```julia
vcat([], [1 2]) # OK
vcat([], [1 "2"]) # Not OK: ERROR: ArgumentError: number of columns of each array must match (got (1, 2))

[[];[1 2]] # OK
[[];[1 "2"]] # Not OK: ERROR: ArgumentError: number of columns of each array must match (got (1, 2))

cat([], cat([], [1 2]; dims=1)) # OK
cat([], cat([], [1 "2"]; dims=1)) # OK

```

```julia
help?> vcat
...

  vcat(A...)

  Concatenate arrays or numbers vertically. Equivalent to cat(A...; dims=1), and to the syntax [a; b; c].

...

```

It’s not accurate to say `vcat(A...)` is equivalent to `cat(A...; dims=1)`.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [April 16, 2024, 7:37am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/2 "2024-04-16T07:37:29Z")

</div>

Hm?

```julia
julia> vcat([], [1 2])
ERROR: DimensionMismatch: number of columns of each array must match (got (1, 2))

```

and

```julia
julia> [[];[1 2]]
ERROR: DimensionMismatch: number of columns of each array must match (got (1, 2))

```

How do you keep finding these things? `cat`ing empty untyped arrays `[]` to other stuff seems like a right code smell to me, maybe you should think about changing your approach?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 16, 2024, 7:53am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/3 "2024-04-16T07:53:33Z")

</div>

> [@Can't concatenate a matrix to \`Any\[\]\` horizontally](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152):
>
> Block1: a = [] b = [1 2; 3 4] a = vcat(a, b) # or a = [a; b] Block2: a = [] b = [1 2; 3 4] a = hcat(a, b) # or a = [a b] Question: Block1 works but Block2 doesn’t. Why?

Thanks. Maybe it is due to Julia version as the post above. Sorry… I was just looking for an approach to recording some process results in a loop. May I ask for some good advice on the following code?

```julia
rec = []
for i = 1:10
    rec = cat(rec, rand(3)'; dims=1)
end
rec

```

Thanks again. 🤝 🤝

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 16, 2024, 7:54am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/4 "2024-04-16T07:54:25Z")

</div>

Following the docstring, these should agree, no?

```julia
julia> A = [[], [1 "2"]]
2-element Vector{Array{Any}}:
 []
 [1 "2"]

julia> cat(A...; dims=1)
1×2 Matrix{Any}:
 1 "2"

julia> vcat(A...)
ERROR: DimensionMismatch: number of columns of each array must match (got (1, 2))
Stacktrace:
     ⋮ internal @ Base
 [3] vcat(::Vector{Any}, ::Matrix{Any})
   @ Base ./abstractarray.jl:1691

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 16, 2024, 7:57am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/5 "2024-04-16T07:57:44Z")

</div>

I think more context would be helpful. What types are the values in each row? What are you doing with them afterwards?

To concatenate 10 rows of three you can do

```julia
julia> mapreduce(vcat, 1:10) do _
           rand(3)'
       end
10×3 Matrix{Float64}:
 0.179634 0.0473286 0.270071
 0.222936 0.761969 0.500733
 0.186272 0.480777 0.0699533
 0.760787 0.509991 0.615817
 0.336471 0.967885 0.307789
 0.939159 0.396305 0.153489
 0.708447 0.370107 0.506113
 0.235929 0.621101 0.791309
 0.549194 0.567354 0.754604
 0.806734 0.507155 0.507556

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 16, 2024, 8:00am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/6 "2024-04-16T08:00:07Z")

</div>

I’m erroring on `vcat([], [1 2])` on v1.10.2, what version is it not erroring on? Also unlike `vcat` and `[[]; [1 2]]`, `cat([], [1 2]; dims=1)` seems to ignore `[]` and does not error. `cat([1], [1 2]; dims=1)` does throw `DimensionMismatch`.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 16, 2024, 8:00am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/7 "2024-04-16T08:00:27Z")

</div>

```julia
julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 24 × AMD Ryzen 9 3900XT 12-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, znver2)
  Threads: 17 on 24 virtual cores
Environment:
  JULIA_NUM_THREADS = 12

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 16, 2024, 8:01am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/8 "2024-04-16T08:01:02Z")

</div>

> [@jar1](#):
>
> Following the docstring, these should agree, no?

Yes, they do.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 16, 2024, 8:04am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/9 "2024-04-16T08:04:05Z")

</div>

> [@jar1](#):
>
> What are you doing with them afterwards?

I just want to write push row vectors to an empty matrix line by line in a loop so that I can analyze it afterwards.

---

<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:** [April 16, 2024, 8:10am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/10 "2024-04-16T08:10:40Z")

</div>

> [@WuSiren](#):
>
> Sorry… I was just looking for an approach to recording some process results in a loop.

The standard approach to record results in a loop is

```julia
rec = [] # Using a correctly typed container is better if performance matters at all.
for i = 1:10
    push!(rec, rand(3))
end

```

If you need to convert this to a 10×3 matrix for later processing you can do so with

```julia
stack(rec, dims = 1)

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 16, 2024, 8:15am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/11 "2024-04-16T08:15:40Z")

</div>

Mine is Julia Version 1.9.0.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 16, 2024, 8:18am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/12 "2024-04-16T08:18:30Z")

</div>

> [@GunnarFarneback](#):
>
> The standard approach

Thank you very much. A standard approach in Julia is just what I need.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [April 16, 2024, 9:01am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/13 "2024-04-16T09:01:26Z")

</div>

> [@WuSiren](#):
>
> Mine is Julia Version 1.9.0.

I can replicate `[[], [1,2]]`, `vcat([], [1 2])`. and `cat([], [1,2]; dims=1)` not throwing an error on both v1.9.0 and v1.9.4. `[[1]; [1 2]]`, `vcat([1], [1 2])`, and `cat([1], [1 2]; dims=1)` throws an error on both versions. I don’t know a way to automatically test a call across all versions so I won’t do that.

This discrepancy between 1.9 and 1.10 should be an issue if it wasn’t already. Presumably someone had changed `vcat` and `[A...]` to not ignore `[]` anymore. Good catch.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 21, 2024, 2:55am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/14 "2024-04-21T02:55:58Z")

</div>

Hi, everyone! I am still wondering if there exists more convenient method if I want to construct a matrix row by row from an empty array in a `for` loop, for example:

```julia
# This seems convenient except I have to define the first (two) row outside the loop:
A = rand(2,3)
for i = 1:9
    A = [A; rand(2,3)]
end

# This is desired (isn't it?) but it doesn't work for all.
A = []
for i = 1:10
    A = [A; rand(2,3)]
end

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 21, 2024, 4:07am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/15 "2024-04-21T04:07:49Z")

</div>

This may be a stopgap for me:

```julia
A =[]
for i = 1:10
    push!(A, rand(2,3))
end
A = cat(A...; dims=1) # or A = vcat(A...)

```

---

<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:** [April 21, 2024, 7:30am UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/16 "2024-04-21T07:30:43Z")

</div>

> [@WuSiren](#):
>
> `A = cat(A...; dims=1) # or A = vcat(A...)`

Just use `stack` unless you need to support Julia versions before 1.9.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 21, 2024, 2:05pm UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/17 "2024-04-21T14:05:25Z")

</div>

But it doesn’t seem to work as I wish:

```julia
julia> A = []
Any[]

julia> push!(A, rand(2,3))
1-element Vector{Any}:
 [0.40938348392403623 0.8614872953758306 0.46182688181820797; 0.518159503368555 0.019077942107566304 0.09225241116879213]

julia> push!(A, rand(2,3))
2-element Vector{Any}:
 [0.40938348392403623 0.8614872953758306 0.46182688181820797; 0.518159503368555 0.019077942107566304 0.09225241116879213]
 [0.876546512326307 0.04790814863432025 0.5461966823100315; 0.8829414621366221 0.727703387550281 0.8668757411333791]

julia> stack(A, dims = 1)
2×2×3 Array{Float64, 3}:
[:, :, 1] =
 0.409383 0.51816
 0.876547 0.882941

[:, :, 2] =
 0.861487 0.0190779
 0.0479081 0.727703

[:, :, 3] =
 0.461827 0.0922524
 0.546197 0.866876

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 21, 2024, 2:45pm UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/18 "2024-04-21T14:45:20Z")

</div>

How about:

```julia
tmpA = Float64[]
for i = 1:10
    append!(tmpA, vec(rand(2,3)))
end
A = reshape(tmpA, :, 3)

```

This method relegates the memory allocation to the Vector (or Memory in 1.11) code. The data undergoes less copying and should therefore be more efficient. Admittedly, it isn’t the most natural way to express the desired output.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 21, 2024, 5:22pm UTC](https://discourse.julialang.org/t/vcat-1-2-ok-vcat-1-2-not-ok/112991/19 "2024-04-21T17:22:22Z")

</div>

> [@WuSiren](#):
>
> `A = cat(A...; dims=1) # or A = vcat(A...)`

`reduce(vcat, A)` is more efficient than splatting for a variable length container, especially if there are lots of rows.

> [@WuSiren](#):
>
> ```julia
> # This is desired (isn't it?) but it doesn't work for all.
> A = []
> for i = 1:10
> A = [A; rand(2,3)]
> end
> 
> ```

You could do e.g. `A = zeros(0,3)` instead of `A = []` to start with an empty `A` that has the correct number of columns (and the correct type).

However, this approach is pretty inefficient because it copies to a new array on every loop iteration — in fact, if you think about it, you should deduce that the cost grows with the square of the number of rows.

If you can compute the number of rows in advance, it’s better to pre-allocate the whole matrix `A` and then fill it in as desired, e.g.

```julia
N = 10
A = Matrix{Float64}(undef, 2N, 3) # pre-allocate
for i = 1:N
    A[2i-1:2i, :] = rand(2,3)
    # or better yet work in-place: @views rand!(A[2i-1:2i, :])
end

```
