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

**URL:** <https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170>\
**Category:** Internals & Design\
**Tags:** proposal\
**Created:** [June 9, 2017, 9:45am UTC](https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170 "2017-06-09T09:45:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![acad2](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@acad2](https://discourse.julialang.org/u/acad2)\
**Post date:** [June 9, 2017, 9:45am UTC](https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170/1 "2017-06-09T09:45:58Z")

</div>

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](https://stackoverflow.com/a/37474579/2338725)) or [`findmax`](https://stackoverflow.com/a/37477316/2338725),

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

```julia
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
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`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html), [`max` or `amax`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.amax.html), [`maximum`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.maximum.html), [`argsort`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html) etc. Wouldn’t it be nice to have these features?[/details]

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [June 9, 2017, 10:27am UTC](https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170/2 "2017-06-09T10:27:51Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 9, 2017, 11:18am UTC](https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170/3 "2017-06-09T11:18:30Z")

</div>

> [@nalimilan](#):
>
> it’s a vector holding two vectors

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/](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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 9, 2017, 11:26am UTC](https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170/4 "2017-06-09T11:26:42Z")

</div>

> [@acad2](#):
>
> Numpy provides a lot of functions: argmax, max or amax, maximum, argsort etc. Wouldn’t it be nice to have these features?

`argmax` → `findmax` or `indmax`, depending.  
`amax` → `maximum`  
`maximum` → `max`  
`argsort` → `sortperm`

---

<div class="post-metadata">

**Author:** ![acad2](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@acad2](https://discourse.julialang.org/u/acad2)\
**Post date:** [June 10, 2017, 3:09am UTC](https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170/5 "2017-06-10T03:09:59Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 10, 2017, 6:27am UTC](https://discourse.julialang.org/t/indmax-and-findmax-issues-with-arrays-with-more-than-1d-edited/4170/6 "2017-06-10T06:27:21Z")

</div>

> [@acad2](#):
>
> x = [8 -4 3; 1 10 -1; 3 12 4]

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
julia> ind2sub(x, 6)
(3,2)

```
