Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

julia
communitycreator

How to concatenate arrays in Julia

Gutha Vamsi Krishna

Overview

An array is a comma-separated, ordered collection of elements. In this shot, we'll learn how to concatenate arrays using Julia.

We can concatenate arrays in Julia using the following functions.

  • vcat: This concatenates along dimension 1.
  • hcat: This concatenates along dimension 2.

Syntax

hcat(pass comma separated arrays)
vcat(pass comma separated arrays)

Parameters

Both the functions will take arrays as parameters.

Return value

Both the functions will return a new array after concatenation.

Example

a = [1 2 3 4 5]
b = [6 7 8 9 10; 11 12 13 14 15]
println("Displaying vcat concatenation")
#concatenation along dimension 1
display(vcat(a,b))

c = [1; 2; 3; 4; 5]
d = [6 7; 8 9; 10 11; 12 13; 14 15]
println("\nDisplaying hcat concatenation")
#concatenation along dimension 2
display(hcat(c,d))

Explanation

  • Line 1–2: We declare and initialize two arrays, a and b.
  • Line 5: We concatenate the arrays a and b along the dimension 1 using the vcat function and display the returned new array.
  • Line 7–8: We declare and initialize two arrays, c and d.
  • Line 11: We concatenate the arrays c and d along the dimension 2 using the hcat function and display the returned new array.

      RELATED TAGS

      julia
      communitycreator
      RELATED COURSES

      View all Courses

      Keep Exploring