# Add row to 2D array in a loop

**URL:** <https://discourse.julialang.org/t/add-row-to-2d-array-in-a-loop/48993>\
**Category:** General Usage\
**Tags:** question, array, loops\
**Created:** [October 25, 2020, 6:49pm UTC](https://discourse.julialang.org/t/add-row-to-2d-array-in-a-loop/48993 "2020-10-25T18:49:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mhex/32/32753_2.png) [@mhex](https://discourse.julialang.org/u/mhex)\
**Post date:** [October 25, 2020, 6:49pm UTC](https://discourse.julialang.org/t/add-row-to-2d-array-in-a-loop/48993/1 "2020-10-25T18:49:30Z")

</div>

Hi,

my aim is to add a new row to a 2D array in a for-loop.

If I execute

A = Array{Int64}(undef, 0, 2)  
A = [A; [1 2]]  
A = [A; [2 3]]  
println(A)

I receive [1 2; 2 3] that is correct. But if I run it in a for loop:

C = Array{Int64}(undef, 0, 2)

for i = 1:5  
global C = [C; [i, i+1]]  
end  
println(C)

I get

ERROR: LoadError: ArgumentError: number of columns of each array must match (got (2, 1))  
Stacktrace:  
[1] \_typed\_vcat(::Type{Int64}, ::Tuple{Array{Int64,2},Array{Int64,1}}) at .\abstractarray.jl:1439  
[2] typed\_vcat at .\abstractarray.jl:1453 [inlined]  
[3] vcat(::Array{Int64,2}, ::Array{Int64,1}) at D:\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.5\SparseArrays\src\sparsevector.jl:1077  
[4] top-level scope at C:\Users\M\Desktop\tsssspp.jl:11  
[5] include(::String) at .\client.jl:457  
[6] top-level scope at REPL[48]:1  
in expression starting at C:\Users\M\Desktop\tsssspp.jl:10

I do not understand the problem.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 25, 2020, 7:07pm UTC](https://discourse.julialang.org/t/add-row-to-2d-array-in-a-loop/48993/2 "2020-10-25T19:07:36Z")

</div>

In your first example, you are concatenating row vectors like `[1 2]`, which works as expected. In the second example, you are concatenating a regular vector like `[i, i+1]`. Note the comma in there which means this is an `Array{T,1}` (a vector), not an `Array{T,2}` (a row of a matrix). If you get rid of that comma, the row concatenation should work.

```julia
julia> C = Array{Int64}(undef, 0, 2)
0×2 Array{Int64,2}

julia> for i = 1:5
           global C = [C; [i i+1]]
       end

julia> C
5×2 Array{Int64,2}:
 1 2
 2 3
 3 4
 4 5
 5 6

```

By the way, it’s easier to read your code if you put it into a code block by surrounding the code with triple backtics ````` on both sides.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 26, 2020, 9:50am UTC](https://discourse.julialang.org/t/add-row-to-2d-array-in-a-loop/48993/4 "2020-10-26T09:50:32Z")

</div>

You may be interested in

[https://github.com/JuliaArrays/ElasticArrays.jl](https://github.com/JuliaArrays/ElasticArrays.jl)
