To further explain the motivation for the above code, findmax
returns a tuple of (maximum value, index), and the .- (0, 1)
part is used to subtract 1 from the index but leave the value unchanged, i.e. change the index from 1-based to 0-based:
julia> r = rand(3)
3-element Array{Float64,1}:
0.43522862419490305
0.7670653549580913
0.4966565072349449
julia> findmax(r)
(0.7670653549580913, 2)
julia> findmax(r) .- (0, 1)
(0.7670653549580913, 1)
I agree with you that it’s not exactly obvious at first sight. A clearer way of writing the same thing would be:
julia> maxval, maxidx = findmax(r)
(0.7670653549580913, 2)
julia> (maxval, maxidx - 1)
(0.7670653549580913, 1)
In your second line, [2]
chooses the index only, and -1
changes it to be 0-based.