# Adding rows to a matrix dynamically

**URL:** <https://discourse.julialang.org/t/adding-rows-to-a-matrix-dynamically/52984>\
**Category:** Performance\
**Tags:** arrays, matrices\
**Created:** [January 7, 2021, 12:43pm UTC](https://discourse.julialang.org/t/adding-rows-to-a-matrix-dynamically/52984 "2021-01-07T12:43:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajesh-Nakka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajesh-nakka/32/20770_2.png) [@Rajesh-Nakka](https://discourse.julialang.org/u/Rajesh-Nakka)\
**Post date:** [January 7, 2021, 12:43pm UTC](https://discourse.julialang.org/t/adding-rows-to-a-matrix-dynamically/52984/1 "2021-01-07T12:43:23Z")

</div>

I am trying to get:= Initialize a matrix of `Float64` then adding rows to it dynamically in a loop!

I tried with `push!` and `append!` but not able to find the right way to do it. I am currently using a not so efficient method where each column of the matrix is constructed as a vector using `push!` and then I making matrix out of those vectors using `hcat`.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 7, 2021, 12:57pm UTC](https://discourse.julialang.org/t/adding-rows-to-a-matrix-dynamically/52984/2 "2021-01-07T12:57:47Z")

</div>

You likely want to build up your matrix column by column instead of row by row, since Julia uses column major storage, as opposed to numpy, for example, which uses a row major layout. This will make appending rows to a matrix inefficient, since a lot of entries will have to be moved around, unless you preallocate a larger array and introduce a greater stride along the first axis. Here is one way to append columns to a matrix without allocating temporary matrices:

```julia
julia> cat!(a, b) = reshape(append!(vec(a), vec(b)), size(a)[1:end-1]..., :)
cat! (generic function with 1 method)

julia> cat!([1 2; 3 4], [1, 2])
2×3 Array{Int64,2}:
 1 2 1
 3 4 2

```

---

<div class="post-metadata">

**Author:** ![Rajesh-Nakka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajesh-nakka/32/20770_2.png) [@Rajesh-Nakka](https://discourse.julialang.org/u/Rajesh-Nakka)\
**Post date:** [January 7, 2021, 1:33pm UTC](https://discourse.julialang.org/t/adding-rows-to-a-matrix-dynamically/52984/3 "2021-01-07T13:33:04Z")

</div>

Thanks @simeonschaub .  
I will try your suggestion!

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [January 7, 2021, 1:41pm UTC](https://discourse.julialang.org/t/adding-rows-to-a-matrix-dynamically/52984/4 "2021-01-07T13:41:35Z")

</div>

If the size of the vectors is known in advance and not too large, you can also build your data as a vector of static vectors, and then view it as a matrix:

```julia
using StaticArrays
vecs = SVector{3,Int}[]
for i in 1:5
    push!(vecs, SVector(1,2,3))
end
m = reshape(reinterpret(Int,vecs),3,:)

```

```julia
3×5 reshape(reinterpret(Int64, ::Array{SArray{Tuple{3},Int64,1,3},1}), 3, 5) with eltype Int64:
 1 1 1 1 1
 2 2 2 2 2
 3 3 3 3 3

```

---

<div class="post-metadata">

**Author:** ![Rajesh-Nakka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rajesh-nakka/32/20770_2.png) [@Rajesh-Nakka](https://discourse.julialang.org/u/Rajesh-Nakka)\
**Post date:** [January 7, 2021, 2:00pm UTC](https://discourse.julialang.org/t/adding-rows-to-a-matrix-dynamically/52984/5 "2021-01-07T14:00:08Z")

</div>

Thanks @yha!

size of the arrays is unknown & it varies from case to case.

I tried with the following code but getting errors

```julia
function test(N::Int64)
    oa = Matrix{Float64}(undef, 0, 2)
    for _ = 1:N
        if some_condition == true
            append!(oa, rand(1, 2))
        end
    end
    return oa
end

ERROR: MethodError: no method matching append!(::Array{Float64,2}, ::Array{Float64,2})
Closest candidates are:
  append!(::BitArray{1}, ::Any) at bitarray.jl:771
  append!(::AbstractArray{T,1} where T, ::Any) at array.jl:981
  append!(::DataStructures.MutableLinkedList, ::Any...) at ~/.julia/packages/DataStructures/5hvIb/src/mutable_list.jl:160

```

Thanks!
