Add row to 2D array in a loop

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

1 Like