Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate 2 Julia Arrays without modifying them

Tags:

arrays

list

julia

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 ?

like image 245
Femto Trader Avatar asked May 13 '16 09:05

Femto Trader


People also ask

How do you concatenate an array in Julia?

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.

Can we concatenate two arrays?

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.

How do you concatenate arrays?

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.


2 Answers

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"
like image 93
niczky12 Avatar answered Sep 27 '22 16:09

niczky12


you can 'join' the arrays by

l3 = [l1; l2]
like image 42
OleEinar Avatar answered Sep 27 '22 17:09

OleEinar