# Can't concatenate a matrix to \`Any\[\]\` horizontally

**URL:** <https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152>\
**Category:** General Usage\
**Tags:** question, matrix\
**Created:** [March 26, 2024, 4:08pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152 "2024-03-26T16:08:51Z")\
**Posts on this page:** 10\
**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:** [March 26, 2024, 4:08pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/1 "2024-03-26T16:08:51Z")

</div>

Block1:

```julia
a = []
b = [1 2; 3 4]
a = vcat(a, b) # or a = [a; b]

```

Block2:

```julia
a = []
b = [1 2; 3 4]
a = hcat(a, b) # or a = [a b]

```

Question: Block1 works but Block2 doesn’t. Why?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 26, 2024, 4:22pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/2 "2024-03-26T16:22:12Z")

</div>

Block 1 doesn’t work for me

```julia
julia> a = []
Any[]

julia> b = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> a = vcat(a, b) # or a = [a; b]
ERROR: DimensionMismatch: number of columns of each array must match (got (1, 2))
Stacktrace:
 [1] _typed_vcat(::Type{Any}, A::Tuple{Vector{Any}, Matrix{Int64}})
   @ Base ./abstractarray.jl:1702

```

---

<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:** [March 26, 2024, 4:22pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/3 "2024-03-26T16:22:25Z")

</div>

Are you sure?

```julia
julia> a = []
Any[]

julia> b = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> a = vcat(a, b) 
ERROR: DimensionMismatch: number of columns of each array must match (got (1, 2))

```

---

<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:** [March 26, 2024, 4:26pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/4 "2024-03-26T16:26:29Z")

</div>

😲

```julia
julia> a = []
Any[]

julia> b = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> a = vcat(a, b)
2×2 Matrix{Any}:
 1 2
 3 4

```

---

<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:** [March 26, 2024, 4:28pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/5 "2024-03-26T16:28:51Z")

</div>

If I run Block2 it reports:

```julia
ERROR: DimensionMismatch: mismatch in dimension 1 (expected 0 got 2)

```

---

<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:** [March 26, 2024, 4:33pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/6 "2024-03-26T16:33:56Z")

</div>

Julia Version 1.9.0

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 26, 2024, 4:35pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/7 "2024-03-26T16:35:35Z")

</div>

I would say this, as a first thought, that being a vector and not a matrix it can also be vertically concatenated with a matrix (which is reinterpreted as a vector?) that has different dimensions.  
While to concatenate horizontally the dimensions must match.  
IF instead of the vector you define an empty matrix the problem also occurs for vcat

```julia
julia> a = [;;]
0×0 Matrix{Any}

julia> b = [1 2; 3 4]
2×2 Matrix{Int64}:
 1 2
 3 4

julia> a = vcat(b, a) # or a = [a; b]
ERROR: DimensionMismatch: mismatch in dimension 2 (expected 2 got 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:** [March 26, 2024, 4:54pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/8 "2024-03-26T16:54:28Z")

</div>

Thanks. Your explanation makes sense to me.

But now how to explain the other more confusing problem: why can’t they run Block1?

---

<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:** [March 26, 2024, 5:03pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/9 "2024-03-26T17:03:38Z")

</div>

Yes looks like this behaviour changed between 1.9 and 1.10.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 26, 2024, 5:18pm UTC](https://discourse.julialang.org/t/cant-concatenate-a-matrix-to-any-horizontally/112152/10 "2024-03-26T17:18:23Z")

</div>

I tried defining `a=Int[]` and this seems to change the behavior between  
`vcat(a,b) and cat(a,b,dims=Val(1))` which should be equivalent

I tried to see what’s behind `cat(a,b,dims=Val(1))`, but it’s too complicated to follow
