Concatenating arrays in Julia

Abhijith picture Abhijith · Sep 20, 2016 · Viewed 35.9k times · Source

If the two Int arrays are, a = [1;2;3] and b = [4;5;6], how do we concatenate the two arrays in both the dimensions? The expected outputs are,

julia> out1
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> out2
3x2 Array{Int64,2}:
 1  4
 2  5
 3  6

Answer

SalchiPapa picture SalchiPapa · Sep 20, 2016

Use the vcat and hcat functions:

julia> a, b = [1;2;3], [4;5;6]
([1,2,3],[4,5,6])

help?> vcat
Base.vcat(A...)

   Concatenate along dimension 1

julia> vcat(a, b)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

help?> hcat
Base.hcat(A...)

   Concatenate along dimension 2

julia> hcat(a, b)
3x2 Array{Int64,2}:
 1  4
 2  5
 3  6