`indmax` and `findmax` issues with arrays with more than 1D (edited)

Let’s say I want to find the position of the maximum element in a 2D array. The closest I found: [indmax] (Location of minimum in Julia - Stack Overflow) or findmax,

which does not work as expected with arrays having more than 1D:

julia> x = [8 -4 3; 1 10 -1; 3 12 4]
3×3 Array{Int64,2}:
 8  -4   3
 1  10  -1
 3  12   4

julia> indmax(x)
6

julia> findmax(x)
(12,6)

As position, it returns one integer, 6. Of course, from this, we can compute the 2D coordinate (3,2). But, wouldn’t it be nice to see the 2D coordinate as output directly? Is it worth reporting an issue?

[details=Click to expand previous comment]which do not work with array having more than 1D:

julia> x = [[8 -4 3], [12 10 -1]]
2-element Array{Array{Int64,2},1}:
 [8 -4 3]
 [12 10 -1]

julia> indmax(x)
ERROR: MethodError: no method matching isless(::Array{Int64,2}, ::Array{Int64,2})
 in findmax(::Array{Array{Int64,2},1}) at .\array.jl:1233
 in indmax(::Array{Array{Int64,2},1}) at .\array.jl:1281

julia> findmax(x)
ERROR: MethodError: no method matching isless(::Array{Int64,2}, ::Array{Int64,2})
 in findmax(::Array{Array{Int64,2},1}) at .\array.jl:1233

  1. Is this worth reporting a bug? Or, I misunderstood anything?
  2. For 1D array, both indmax and findmax return a 2 element array, the second element being the position. Depending on the situation, which may not be sufficient (e.g., to find the frequency of the maximum element; find the maximum element along one particular axis). Numpy provides a lot of functions: argmax, max or amax, maximum, argsort etc. Wouldn’t it be nice to have these features?[/details]

The problem is that your x is not a matrix, it’s a vector holding two vectors. Try with:

x = [8 -4 3; 12 10 -1]
1 Like

To be precise, it’s actually a vector holding two matrices.

I suggest that the OP take a look here (https://docs.julialang.org/en/stable/manual/arrays/) for info about construction of arrays, since s/he seems to be mixing up Julia and Python syntax.

1 Like

argmaxfindmax or indmax, depending.
amaxmaximum
maximummax
argsortsortperm

1 Like

@nalimilan @DNF
Thanks for your kind explanation. I guess that’s not uncommon for someone using in Python for a long time :slight_smile:.

However, I still see issues with findmax and indmax. Please check my edited comment.

Please do not edit previous posts; that gets confusing.

Do you know that there is an ind2sub function for finding the 2D (or n-D) coordinates?

julia> ind2sub(x, 6)
(3,2)