46

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
Abhijith
  • 1,158
  • 1
  • 13
  • 27
  • 2
    Possible duplicate of [Concatenate 2 Julia Arrays without modifying them](http://stackoverflow.com/questions/37206550/concatenate-2-julia-arrays-without-modifying-them) – niczky12 Sep 20 '16 at 07:36

5 Answers5

44

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
HarmonicaMuse
  • 7,633
  • 37
  • 52
34

Square brackets can be used for concatenation:

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

julia> [a; b]
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

julia> [a b]
3×2 Array{Int64,2}:
 1  4
 2  5
 3  6
user6847814
  • 456
  • 3
  • 4
  • 3
    This is syntactic sugar for `vcat` and `hcat` respectively: `[e.head for e in [:([a; b]), :([a b])]] # Symbol[:vcat,:hcat]` – HarmonicaMuse Sep 20 '16 at 07:00
  • 12
    Generally I think `vcat` and `hcat` should be preferred because this solution is whitespace sensitive. For example: `[a - b] ` will `vcat` while `[a -b]` will `hcat`. That can be a nasty bug to find. – Chris Rackauckas Sep 20 '16 at 07:29
  • 7
    It seems a bit backward to *not* prefer the syntactic sugar version. After all, what's the sugar *for*? Are you saying that this syntax will probably be removed? – DNF Sep 20 '16 at 10:19
17

You can use the cat function to concatenate any number of arrays along any dimension. The first input is the dimension over which to perform the concatenation; the remaining inputs are all of the arrays you wish to concatenate together

a = [1;2;3]
b = [4;5;6]

## Concatenate 2 arrays along the first dimension
cat(1,a,b)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

## Concatenate 2 arrays along the second dimension
cat(2,a,b)
3x2 Array{Int64,2}:
 1  4
 2  5
 3  6

## Concatenate 2 arrays along the third dimension
cat(3,a,b)
3x1x2 Array{Int64,3}:
[:, :, 1] =
 1
 2
 3

[:, :, 2] =
 4
 5
 6
Landon
  • 828
  • 8
  • 23
2

when encountered Array{Array,1}, the grammer is a little bit different, like this:

julia> a=[[1,2],[3,4]]
2-element Array{Array{Int64,1},1}:
 [1, 2]
 [3, 4]

julia> vcat(a)
2-element Array{Array{Int64,1},1}:
 [1, 2]
 [3, 4]

julia> hcat(a)
2×1 Array{Array{Int64,1},2}:
 [1, 2]
 [3, 4]

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

julia> hcat(a...)
2×2 Array{Int64,2}:
 1  3
 2  4

ref:

... combines many arguments into one argument in function definitions In the context of function definitions, the ... operator is used to combine many different arguments into a single argument. This use of ... for combining many different arguments into a single argument is called slurping

Jason D
  • 193
  • 1
  • 7
1

Functional way to concatanate 2 arrays is to use reduce function.

a = rand(10, 1)
b = rand(10, 1)
c = reduce(hcat, [ a, b])
Kadir Gunel
  • 309
  • 3
  • 14
  • For two arrays it's not that important, but when you want to concatenate multiple arrays, this method is an _enormous_ performance gain over `hcat(arrays...)`. – KeithWM Feb 20 '23 at 21:53