getindex() 是 julia 中的一個內置函數,用於構造指定類型的數組。此函數還用於獲取特定索引處的數組元素。
用法: getindex(type, elements…])
參數:
- type:指定類型。
- elements:指定的元素列表。
返回值:它返回指定類型的構造數組以及特定索引處的數組元素。
範例1:
Python
# 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))
輸出:
範例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)
輸出:
相關用法
- Julia foreach()用法及代碼示例
- Julia isassigned()用法及代碼示例
- Julia Array fill()用法及代碼示例
- Julia first()用法及代碼示例
- Julia last()用法及代碼示例
- Julia findmax()用法及代碼示例
- Julia findmin()用法及代碼示例
- Julia Array findprev()用法及代碼示例
- Julia Array findnext()用法及代碼示例
- Julia Array findfirst()用法及代碼示例
- Julia Array findlast()用法及代碼示例
- Julia eachindex()用法及代碼示例
- Julia setindex!()用法及代碼示例
- Julia first()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Accessing element at a specific index in Julia – getindex() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。