在 Julia 中获取集合的第一个索引 – firstindex() 方法
firstindex()
是 julia 中的一个内置函数,用于返回指定集合的第一个索引。如果给定维度d ,则返回沿维度 d 的集合的第一个索引。
Syntax:
firstindex(collection)
or
firstindex(collection, d)
Parameters:
- collection: Specified collection.
- b: Specified dimension.
Returns: It returns the first index of the specified collection. If a dimension d is given, return the first index of collection along dimension d.
示例 1:
# Julia program to illustrate
# the use of firstindex() method
# Getting the first index of the specified collection.
println(firstindex(0))
println(firstindex([1, 2, 3]))
println(firstindex([2 4; 5 6]))
println(firstindex([3; 5; 7; 9]))
输出:
示例 2:
# Julia program to illustrate
# the use of firstindex() method
# Getting the first index of the specified
# collection. If a dimension d is given,
# return the first index of collection
# along dimension d.
println(firstindex(rand(1, 2, 3, 4), 1))
println(firstindex(rand(1, 2, 3, 4), 2))
println(firstindex([1, 2, 3], 2))
println(firstindex([2 4; 6 8], 2))
println(firstindex([2; 4; 6; 8], 2))
println(firstindex(cat([1 2; 3 4], [5 6; 7 8], [2 2; 3 4], dims = 3);))
输出: