# Concatenating matrices

**URL:** https://discourse.julialang.org/t/concatenating-matrices/19157
**Category:** General Usage
**Created:** [December 31, 2018, 10:19pm UTC](https://discourse.julialang.org/t/concatenating-matrices/19157 "2018-12-31T22:19:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [December 31, 2018, 10:19pm UTC](https://discourse.julialang.org/t/concatenating-matrices/19157/1 "2018-12-31T22:19:24Z")

</div>

I have a list of matrices passed into a function. I want to vertically concatenate them into a single matrix. I used to be able to do this:

```
foo = [[1 2; 3 4; 5 6], [7 8; 9 10; 11 12]]
A = []
for m in foo
    A = [A; m]
end

```

which, ok, is inefficient, but in this particular code I only have to do it once so I don’t care. And it has the advantage of being fairly readable and compact.

As of 1.0, this throws an error:

```
ERROR: DimensionMismatch("All inputs to vcat should have the same number of columns")

```

Actually, in the REPL the error is different:

```
ERROR: UndefVarError: A not defined

```

The preferred idiom appears to be something along the lines of:

```
foo = [[1 2; 3 4; 5 6], [7 8; 9 10; 11 12]]
A = []
for m in foo
    append!(A, m)
end
A = reshape(A, :, size(foo[1], 2))

```

…because append! doesn’t preserve the shape of multidimensional arrays. This is ugly. And it doesn’t work if foo is empty, or if the first element in foo is one-dimensional or if the elements in foo are scalars. Which, of course, means that now I have to write lots of error checking code, making this even uglier.

In this particular case I’d gladly trade efficiency for compactness. Is there a better way to do this?

---

<div class="post-metadata">

### Author: ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)
#### Post date: [December 31, 2018, 10:29pm UTC](https://discourse.julialang.org/t/concatenating-matrices/19157/2 "2018-12-31T22:29:48Z")

</div>

Does this accomplish what you want?

```julia
julia> vcat(foo...)
6×2 Array{Int64,2}:
  1 2
  3 4
  5 6
  7 8
  9 10
 11 12

```

The performant idiom for this is `reduce(vcat, foo)`, which is efficient due to a specialization of `reduce` for `vcat` that avoids unnecessary intermediate allocations.

---

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [December 31, 2018, 10:36pm UTC](https://discourse.julialang.org/t/concatenating-matrices/19157/3 "2018-12-31T22:36:08Z")

</div>

Huh. Yes, that’s brilliant.

---

<div class="post-metadata">

### Author: ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)
#### Post date: [January 1, 2019, 10:55am UTC](https://discourse.julialang.org/t/concatenating-matrices/19157/4 "2019-01-01T10:55:13Z")

</div>

I have a vague memory of Stefan karpinski mentioning in a post somewhere that splatting is not the best solution if the input list is long since it effectively results in high-dimensional multiple dispatch. Not sure if this still applies to v1.0 but would be interested to find out since I use this syntax in my code quite a bit (and I also have a dedicated function for long list inputs that preallocates the output matrix and then assigns the inputs to it sequentially)
