 # Adding rows to a matrix dynamically

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`.

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> 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
``````
2 Likes

Thanks @simeonschaub .

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:

``````using StaticArrays
vecs = SVector{3,Int}[]
for i in 1:5
push!(vecs, SVector(1,2,3))
end
m = reshape(reinterpret(Int,vecs),3,:)
``````
``````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
``````
2 Likes

Thanks @yha!

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

I tried with the following code but getting errors

``````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