# How to do in place concatenation of matrices

**URL:** <https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 6, 2021, 11:24am UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610 "2021-03-06T11:24:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DhananjayAshok](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhananjayashok/32/22554_2.png) [@DhananjayAshok](https://discourse.julialang.org/u/DhananjayAshok)\
**Post date:** [March 6, 2021, 11:24am UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610/1 "2021-03-06T11:24:50Z")

</div>

Hi there. I have a matrix a that I want to maintain and accumilate more data points to over time. Assume I have a given below and I want to append b to it without having to do a new assignment on a. How do I do so?

```julia
a = rand(2, 100)
b = rand(2, 10)
push!(a, b) # Does not work
append!(a, b) #Does not work
cat(a, b, dims=2) #Works but does not work in place i.e change a
cat!(a, b, dims=2) # is not defined

```

---

<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:** [March 6, 2021, 1:57pm UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610/2 "2021-03-06T13:57:35Z")

</div>

See [GitHub - JuliaArrays/ElasticArrays.jl: Resizeable multi-dimensional arrays for Julia](https://github.com/JuliaArrays/ElasticArrays.jl)

---

<div class="post-metadata">

**Author:** ![DhananjayAshok](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dhananjayashok/32/22554_2.png) [@DhananjayAshok](https://discourse.julialang.org/u/DhananjayAshok)\
**Post date:** [March 6, 2021, 4:14pm UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610/3 "2021-03-06T16:14:33Z")

</div>

Thank you for the reply! I will look into that, but just wanted to confirm is there no (perhaps even sub-optimal way) to this using plain Arrays? I am working off a repo that I do not fully control and if I changed the arrays to elastic arrays I would have to change a lot of the code and make keeping up with new changes in the main repo very difficult.

---

<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:** [March 6, 2021, 5:11pm UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610/4 "2021-03-06T17:11:01Z")

</div>

Maybe you could explain a little bit further the problem. Of course you can (not in-place) do

```julia
a = cat(a,b,dims=2)

```

and go on.

That could only be in place while keeping `a` contiguous in memory if there was some memory reserved for that growth in advance (is that what ElasticArrays does?). If there isn’t, either `a` has to be reallocated, or it would become split in memory, requiring multiple pointers, like a vector of arrays. If one or the other is best for performance depends on what is going to be made with `a` afterwards, but if it is going to be any heavy computation, probably reallocating it is the best choice.

---

<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:** [March 6, 2021, 5:42pm UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610/5 "2021-03-06T17:42:00Z")

</div>

> [@lmiq](#):
>
> That could only be in place while keeping `a` contiguous in memory if there was some memory reserved for that growth in advance (is that what ElasticArrays does?).

ElasticArrays, like the built in `push!` for 1d arrays, reserves memory in advance by geometrically increasing (e.g. doubling) the allocation each time it runs out of space. This allows you to simply `push!` or concatenate repeatedly with amortized linear cost.

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [March 6, 2021, 7:44pm UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610/6 "2021-03-06T19:44:48Z")

</div>

Do you know (approximately) how large your array will get?  
Reserving memory in advance is probably the only way to truly avoid multiple allocations.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 6, 2021, 8:00pm UTC](https://discourse.julialang.org/t/how-to-do-in-place-concatenation-of-matrices/56610/7 "2021-03-06T20:00:47Z")

</div>

Depending on your use case a Vector of Vectors is a good option.
