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