Location of the minimum in Julia

Does Julia have a build team to find the minimum index of a vector? R, for example, has a command which.min(and which.max, of course).

Obviously, I could write the following myself, but it would be nice not to do this.

function whichmin( x::Vector )
  i = 1
  min_x=minimum(x)
  while( x[i] > min_x ) 
    i+=1 
  end
  return i
end

Sorry if this was asked before, but I could not find it. Thank!

+4
source share
3 answers

I believe that indmax(itr)does what you want. From the julia documentation:

indmax (itr) β†’ Integer

Returns the index of the maximum item in the collection.

And here is a usage example:

julia> x = [8, -4, 3.5]
julia> indmax(x)
1
+9
source

There is also findmax, which returns both the maximum value and its position.

+7
source

:

x = rand(1:9, 2,3)
# 2Γ—3 Array{Int64,2}:
#  5  1  9
#  3  3  8

indmin(x)
# 3 
# => third element in the column-major ordered array (value=1)

ind2sub(size(x),indmin(x))
# (1, 2)
# => (row,col) indexes: what you are looking for.

-

0

All Articles