Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 817
Alec Avatar asked Nov 23 '25 21:11

Alec


1 Answers

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
like image 190
mcabbott Avatar answered Nov 26 '25 15:11

mcabbott



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!