I would like to concatenate 2 arrays.
julia> l1=["a","b"]
2-element Array{ASCIIString,1}:
"a"
"b"
julia> l2=["c","d"]
2-element Array{ASCIIString,1}:
"c"
"d"
append!
can do this but this function is modifying l1
(that's a function named with a !
)
julia> append!(l1, l2)
4-element Array{ASCIIString,1}:
"a"
"b"
"c"
"d"
julia> l1
4-element Array{ASCIIString,1}:
"a"
"b"
"c"
"d"
I was looking for a append
function (without exclamation point).
But such a function doesn't seems to exist.
Any idea ?
The hvcat() is an inbuilt function in julia which is used to concatenate the given arrays horizontally and vertically in one call. The first parameter specifies the number of arguments to concatenate in each block row.
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length aLen + bLen . Now, in order to combine both, we copy each element in both arrays to result by using arraycopy() function.
In addition to @oleeinar's answer, you can use hcat
and vcat
to concatenate arrays:
l3 = vcat(l1, l2)
4-element Array{ASCIIString,1}:
"a"
"b"
"c"
"d"
You can also concatenate horizontally with hcat
:
l4 = hcat(l1, l2)
2x2 Array{ASCIIString,2}:
"a" "c"
"b" "d"
you can 'join' the arrays by
l3 = [l1; l2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With