问题描述
Julia 是否有内置命令来查找向量最小值的索引?例如,R 有一个 which.min
命令(当然还有一个 which.max
).
Does Julia have a build in command to find the index of the minimum of a vector? R, for example, has a which.min
command (and a which.max
, of course).
显然,我可以自己编写以下内容,但最好不必这样做.
Obviously, I could write the following myself, but it would be nice not to have to.
function whichmin( x::Vector )
i = 1
min_x=minimum(x)
while( x[i] > min_x )
i+=1
end
return i
end
抱歉,如果以前有人问过这个问题,但我找不到.谢谢!
Apologies if this has been asked before, but I couldn't find it. Thanks!
推荐答案
从 0.7-alpha 开始,indmin
和 indmax
已被弃用.使用 argmin
和 argmax
而是.
Since 0.7-alpha, indmin
and indmax
are deprecated.Use argmin
and argmax
instead.
对于向量,它只返回线性索引
For a vector it just returns the linear index
julia> x = rand(1:9, 4)
4-element Array{Int64,1}:
9
5
8
5
julia> argmin(x)
2
julia> argmax(x)
1
如果同时查找索引和值,请使用 findmin
和 findmax
.
If looking for both the index and the value, use findmin
and findmax
.
对于多维数组,所有这些函数都返回笛卡尔索引.
For multidimensional array, all these functions return the CartesianIndex.
这篇关于Julia中最小值的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!