Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does julia have an equivalent function to hstack in python?

Tags:

julia

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?

like image 991
mooglin Avatar asked Aug 27 '26 20:08

mooglin


1 Answers

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
like image 70
Przemyslaw Szufel Avatar answered Sep 01 '26 00:09

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!