在 Julia 中访问特定索引处的元素 – getindex() 方法
getindex()是 julia 中的一个内置函数,用于构造指定类型的数组。此函数还用于获取特定索引处的数组元素。
Syntax: getindex(type, elements…])
Parameters:
- type: Specified type.
- elements: Specified list of elements.
Returns: It returns the constructed array of the specified type and also the element of the array at a specific index.
示例 1:
Python
# Julia program to illustrate
# the use of Array getindex() method
# Constructing 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))
Python
# 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))
Python
# 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)
输出:
示例 2:
Python
# 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))
输出:
示例 3:
Python
# 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)
输出: