Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two vectors in Julia?

Given two vectors a = [1, 2] and b = [3, 4], how do I obtain the concatenated vector c = [1, 2, 3, 4]? It seems that hcat or vcat could be used as they work on arrays, but when using vectors to store collections of elements it seems unfitting to first think about the orientation of the data; it's just supposed to be a list of values.

like image 575
Samuel Avatar asked Feb 03 '26 00:02

Samuel


1 Answers

You can write

[a; b]

Under the hood this is the same as vcat, but it's terser, looks better, and is easier to remember, as it's also consistent with literal matrix construction syntax.

An alternative for concatenating multiple vectors is

reduce(vcat, (a, b))
like image 115
DNF Avatar answered Feb 05 '26 18:02

DNF



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!