在 Julia 中查找最大元素及其索引 – findmax() 方法
findmax()
是 julia 中的内置函数,用于返回指定集合的最大元素及其索引。如果集合中存在多个最大元素,则将返回第一个。如果有任何数据元素为 NaN,则返回该元素。
Syntax:
findmax(itr)
or
findmax(A; dims)
Parameters:
- itr: Specified collection of elements.
- A: Specified array.
- dims: Specified dimension.
Returns: It returns the maximum elements with their corresponding index.
示例 1:
# Julia program to illustrate
# the use of findmax() method
# Getting the maximum elements
# with their corresponding index.
println(findmax([1, 2, 3, 4]))
println(findmax([5, 0, false, 6]))
println(findmax([1, 2, 3, true]))
println(findmax([5, 0, NaN, 6]))
println(findmax([1, 2, 3, 3]))
输出:
示例 2:
# Julia program to illustrate
# the use of findmax() method
# Getting the value and index of
# the maximum over the given dimensions
A = [5 10; 15 20];
println(findmax(A, dims = 1))
println(findmax(A, dims = 2))
输出: