當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Julia getindex()用法及代碼示例


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)

輸出:




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Accessing element at a specific index in Julia – getindex() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。