Accessing element at a specific index in Julia – getindex() Method
The getindex()
is an inbuilt function in julia which is used to construct array of the specified type. This function is also used to get the element of the array at a specific index.
Syntax: getindex(type, elements…])
Parameters:
- type: Specified type.
- elements: Specified list of elements.
Returns: It returns the costructed array of the specified type and also the element of the array at a specific index.
Example 1:
# Julia program to illustrate # the use of Array getindex() method # Costructing array of the different types println(getindex(Int8, 1 , 2 , 3 )) println(getindex(Int16, 5 , 10 , 15 , 20 )) println(getindex(Int32, 2 , 4 , 6 , 8 , 10 )) |
chevron_right
filter_none
Output:
Example 2:
# Julia program to illustrate # the use of Array getindex() method # Getting an element of the 2D array # A at some specific indexs of (2, 3) # and (2, 2) A = [ 1 2 3 ; 4 5 6 ] println(getindex(A, 2 , 2 )) println(getindex(A, 2 , 3 )) |
chevron_right
filter_none
Output:
Example 3:
# Julia program to illustrate # the use of Array getindex() method # Getting an element of the 3D array # A at some specific indexs of (1, 2, 2) # and (1, :, 2) A = cat([ 1 2 ; 3 4 ], [ "hello" "Geeks" ; "Welcome" "GFG" ], [ 5 6 ; 7 8 ], dims = 3 ) getindex(A, 1 , 2 , 2 ) getindex(A, 1 , :, 2 ) |
chevron_right
filter_none
Output: