Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating arrays in Julia

Tags:

julia

If the two Int arrays are, a = [1;2;3] and b = [4;5;6], how do we concatenate the two arrays in both the dimensions? The expected outputs are,

julia> out1 6-element Array{Int64,1}:  1  2  3  4  5  6  julia> out2 3x2 Array{Int64,2}:  1  4  2  5  3  6 
like image 750
Abhijith Avatar asked Sep 20 '16 06:09

Abhijith


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.

How do I concatenate strings in Julia?

Using '*' operator It is used to concatenate different strings and/or characters into a single string. We can concatenate two or more strings in Julia using * operator.

How do you define a vector in Julia?

A Vector in Julia can be created with the use of a pre-defined keyword Vector() or by simply writing Vector elements within square brackets([]). There are different ways of creating Vector. vector_name = [value1, value2, value3,..] or vector_name = Vector{Datatype}([value1, value2, value3,..])


1 Answers

Use the vcat and hcat functions:

julia> a, b = [1;2;3], [4;5;6] ([1,2,3],[4,5,6])  help?> vcat Base.vcat(A...)     Concatenate along dimension 1  julia> vcat(a, b) 6-element Array{Int64,1}:  1  2  3  4  5  6  help?> hcat Base.hcat(A...)     Concatenate along dimension 2  julia> hcat(a, b) 3x2 Array{Int64,2}:  1  4  2  5  3  6 
like image 105
HarmonicaMuse Avatar answered Nov 02 '22 20:11

HarmonicaMuse