Alec
Alec

Reputation: 4482

How to concatenate differently sized vectors in Julia?

How can I concatenate arrays of different size with a "filler" value where the arrays don't line up?

a = [1,2,3]
b = [1,2]

And I would like:

[1 2 3
 1 2 missing]

Or

[1 2 3
 1 2 nothing]

Upvotes: 3

Views: 412

Answers (1)

mcabbott
mcabbott

Reputation: 2580

One way, using rstack which is "ragged stack". It always places arrays along one new dimension, thus given vectors, they form the columns of a matrix. (The original question may want the transpose of this result.)

julia> using LazyStack

julia> rstack(a, b; fill=missing)
3×2 Matrix{Union{Missing, Int64}}:
 1  1
 2  2
 3   missing

julia> rstack(a, b, reverse(a), reverse(b); fill=NaN)
3×4 Matrix{Real}:
 1    1  3    2
 2    2  2    1
 3  NaN  1  NaN

Upvotes: 6

Related Questions