Using push! to add a new row of data

You can only push to vectors, but not to multi-dimensional arrays. (See for example here Resize!(matrix) - #2 by StefanKarpinski why it is this way.)

  • If you really need to add rows dynamically, you could consider using a Vector of Vectors
LL = Vector{Float64}[]
push!(LL, [1.0, 2.0])

or maybe you could use Introduction · DataFrames.jl or GitHub - JuliaArrays/ElasticArrays.jl: Resizeable multi-dimensional arrays for Julia

  • If you only need to add a few rows/columns, you could use vcat or hcat. For example
LL = [X[:] Y[:]]   # or hcat(X[:],Y[:])  or  hcat(vec(X),vec(Y))
  • If you know in advance how many rows you need, that is of course ideal and it can sometimes give great performance benefits.
LL = fill(NaN, n*m, 2)  # or LL = Array{Float64}(undef, n*m, 2)
k = 1
for j ∈ 1:n #180
    for i ∈ 1:m #360
        LL[k, :] .= (X[i,j], Y[i,j])
        k += 1
    end
end
3 Likes