mooglin
mooglin

Reputation: 534

Does julia have an equivalent function to hstack in python?

I would like to construct a 2d array of size n x d using d columns vectors of length n.

Does julia have such a function or something similar?

Upvotes: 3

Views: 433

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

Suppose you have 2 columns:

julia> columns = [rand(1:20,3) for _ in 1:2]
2-element Vector{Vector{Int64}}:
 [20, 3, 19]
 [9, 11, 18]

hcat does the job taking each column:

julia> hcat(columns[1], columns[2])
3×2 Matrix{Int64}:
 20   9
  3  11
 19  18

You can also just pass the entire vector using ... operator:

julia> hcat(columns...)
3×2 Matrix{Int64}:
 20   9
  3  11
 19  18

The Matrix syntax also supports building one from columns:

julia> [columns[1] columns[2]]
3×2 Matrix{Int64}:
 20   9
  3  11
 19  18

Upvotes: 3

Related Questions