Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location of minimum in Julia

Tags:

julia

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!

like image 259
squipbar Avatar asked May 27 '16 03:05

squipbar


2 Answers

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

If looking for both the index and the value, use findmin and findmax.

For multidimensional array, all these functions return the CartesianIndex.

like image 165
ederag Avatar answered Sep 19 '22 21:09

ederag


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

indmax(itr) → Integer

Returns the index of the maximum element in a collection.

And here's an example of it in use:

julia> x = [8, -4, 3.5]
julia> indmax(x)
1
like image 42
shane Avatar answered Sep 19 '22 21:09

shane