# The behavior of appending an empty matrix to a non-empty one

**URL:** <https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [December 15, 2021, 2:04am UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107 "2021-12-15T02:04:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 15, 2021, 2:04am UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/1 "2021-12-15T02:04:01Z")

</div>

For example,  
`A = [1 2 3; 4 5 6];`

B can be either a N x 3 matrix or empty like the below:  
`B = [];`

When B is empty, why couldn’t I append B to A  
`C = vcat(A, B);`

Error message:  
`ArgumentError: number of columns of each array must match (got (3, 1))`

Instead of throwing out an error, do you think Julia should instead handle this situation differently, i.e., the above equation of `C = vcat(A, B)` should create a C that is equal to A, when B is empty?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 15, 2021, 2:31am UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/2 "2021-12-15T02:31:23Z")

</div>

B is the wrong shape, so it should error.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [December 15, 2021, 2:55am UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/3 "2021-12-15T02:55:24Z")

</div>

What’s the advantages of requiring an empty matrix to have the same column width in order to be able to be appendable?

Other languages, e.g., Matlab can handle this situation well.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [December 15, 2021, 3:28am UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/4 "2021-12-15T03:28:54Z")

</div>

What would be the shape of concatenating the empty matrix with the empty vector?

An error is a useful result because it alerts the user that something unintended may be happening in their code. Handling it well is throwing an error.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 15, 2021, 1:24pm UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/5 "2021-12-15T13:24:23Z")

</div>

One issue here is that there is no single empty matrix—as long as one of the dimensions is zero, you have an empty matrix. The point of requiring the corresponding dimensions to match is consistency and catching logical errors in your code sooner rather than later. What is the condition under which it is possible to vertically concatenate two matrices? Simple: when they have the same number of columns. @leon, you are advocating for making that simple rule more complicated by ignoring the number of columns if the number of rows of one of the matrices is zero. Not only does that complicate a very simple rule, but it can hide logical errors in your code. Getting a matrix with zero rows is a natural edge case, but even then, if your code is correct in general and it concatenates two matrices they should still have the same number of columns even if one or both have no rows. There is no natural edge case where one of the matrices has the wrong number of columns. If column counts disagree, you should get an error to let you know that your code has some mistake in it. The proposed complex rule also brings up more questions. For example, if you concatenate two matrices with zero rows and different numbers of columns, how many columns should the resulting matrix have?

---

<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:** [December 15, 2021, 3:14pm UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/6 "2021-12-15T15:14:16Z")

</div>

One quirk is that `hcat()` returns `[]`. In something like this, you’d prefer a 3×0 Matrix, but of course it doesn’t know that:

```julia
julia> fn(n) = hcat(hcat((fill(i,3) for i in 1:n)...), hcat((fill(10i,3) for i in n+1:10)...));

julia> fn(4)
3×10 Matrix{Int64}:
 1 2 3 4 50 60 70 80 90 100
 1 2 3 4 50 60 70 80 90 100
 1 2 3 4 50 60 70 80 90 100

julia> fn(0)
ERROR: ArgumentError: number of rows of each array must match (got (0, 3))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 15, 2021, 4:35pm UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/7 "2021-12-15T16:35:33Z")

</div>

That seems to be asking too much from Julia. The user should take care of the edge case. For example:

```julia
fn(n) = hcat(reshape(hcat((fill(i,3) for i in 1:n)...),3,n), hcat((fill(10i,3) for i in n+1:10)...));

```

---

<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:** [December 15, 2021, 4:47pm UTC](https://discourse.julialang.org/t/the-behavior-of-appending-an-empty-matrix-to-a-non-empty-one/73107/8 "2021-12-15T16:47:34Z")

</div>

Sure, although this makes a `Matrix{Any}`. Perhaps my complaint is that `hcat()` is defined at all, but returns something which isn’t a neutral element. I presume this is why there is no definition for `*()`.
