Array Literals in Julia
Last Updated :
11 Nov, 2022
Julia is a high performance, dynamic programming language that is easy to use as it has a high-level syntax. It is free for everyone to use. Julia does not give a lot of attention to implementing arrays, so there is a lesser load on the compiler. An array in Julia is represented in a multi-dimensional grid and can contain any type of data. Julia allows us to create arrays not only by creating array variables but also as literals. We will discuss various features of the array literals offered by Julia in this article.
Array Literals
Arrays can be directly constructed in Julia without creating a corresponding variable with the use of square braces with the syntax – [element1, element2, . . . ]. This creates a one-dimensional array where the elements are the ones that are separated by commas in the arguments as shown below:
Type of an array literal
The type of the array is determined by the type of the elements present in it. If all the elements are of the same type, then the array type will be the same. But if they are different, to get a definitive type for the array we can convert the types of the elements to their promotion types using promote() function.
Julia
promote( 12 , 2.3 , 4 / / 5 )
[ 12 , 2.3 , 4 / / 5 ]
|
If we do not perform this promoting operation an array that can hold any type is produced as shown below:
Concatenation of Arrays
Vertical concatenation
The concatenation of elements in an array is performed by placing a semicolon ( ; ) or a new line between them. These elements will be vertically concatenated. But if a comma is placed between the elements instead of the semicolon, Julia just considers them as elements as shown below:
As mentioned above we’re separating the elements with a semicolon to perform vertical concatenation:
We could also just represent the array in new lines to perform the concatenation vertically as shown below:
Horizontal concatenation
To concatenate the array horizontally we place tabs or spaces between the elements instead of commas. If we do not place the tabs Julia considers the whole argument as one element. We concatenate various types of elements like range elements, array elements, and normal numbers as shown below:
Julia
[ 1 : 2 3 : 4 5 : 6 ]
[[ 1 , 2 ] [ 3 , 4 ] [ 5 , 6 ]]
[ 11 22 33 ]
|
Multi-directional concatenation
To concatenate both horizontally and vertically we can place newlines and spacing between the elements respectively together without placing commas as shown below:
The following example shows the use of zeros() function to fill up some spaces with zeros in a 2D space in the array, vertically concatenate 3 and 6 by placing a semi-colon, a new line to concatenate vertically, and spacing to concatenate 7, 8, and 9 horizontally, which ultimately produces a 3×3 matrix.
Julia
[zeros( Int , 2 , 2 ) [ 3 ; 6 ]
[ 7 8 ] 9 ]
|
Typed array literals
An array of a specific type can be generated with the syntax – T[element1, element2, . . . ], where T is the type you want the array to be. Implementing an array with this syntax creates an array which initializes the type to all the elements in it. The following example shows the initialization of the Int8 type to the elements of the array, but we can also put any other type we desire.
Julia
[[ 1 3 ] [ 2 4 ]]
Int8[[ 1 3 ] [ 2 4 ]]
Any [[ 1 3 ] [ 2 4 ]]
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...