# Argmax() -\> vector of index result

**URL:** https://discourse.julialang.org/t/argmax-vector-of-index-result/76532
**Category:** General Usage
**Tags:** maxima
**Created:** [February 16, 2022, 4:13am UTC](https://discourse.julialang.org/t/argmax-vector-of-index-result/76532 "2022-02-16T04:13:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [February 16, 2022, 4:13am UTC](https://discourse.julialang.org/t/argmax-vector-of-index-result/76532/1 "2022-02-16T04:13:44Z")

</div>

What is the elegant solution for argmax? I was expecting that argmax just return indexes of maximum value, but it returns … … … (As it works in MATLAB for instance).

```julia

am=argmax(randn(100,100,dims=2)
mi=map(x->x[2],am)

```

Is there any elegant way, how to rewrite the code in one line?

thank you.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [February 16, 2022, 5:42am UTC](https://discourse.julialang.org/t/argmax-vector-of-index-result/76532/2 "2022-02-16T05:42:21Z")

</div>

I don’t know if more elegant or more efficient than yours, but a little different

```julia
argmax.(eachrow(rand(100,100)))

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [February 16, 2022, 7:44am UTC](https://discourse.julialang.org/t/argmax-vector-of-index-result/76532/3 "2022-02-16T07:44:06Z")

</div>

`last.(argmax(rand(100, 100); dims=2))`

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 16, 2022, 8:08am UTC](https://discourse.julialang.org/t/argmax-vector-of-index-result/76532/4 "2022-02-16T08:08:33Z")

</div>

@gustaphe, fyi, it isn’t working here… neither on 1.7.1 nor on 1.7.2.  
Need to do instead: `last.(Tuple.(argmax(M,dims=2))) `

Or use equivalent command:

```julia
getindex.(argmax(M; dims=2), 2)

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [February 16, 2022, 8:17am UTC](https://discourse.julialang.org/t/argmax-vector-of-index-result/76532/5 "2022-02-16T08:17:23Z")

</div>

Huh, that’s surprising. Oh well, the `get(..., 2)` method is more explicit anyway

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [February 16, 2022, 8:24am UTC](https://discourse.julialang.org/t/argmax-vector-of-index-result/76532/6 "2022-02-16T08:24:08Z")

</div>

> but it returns … … …

It returns `CartesianIndex` values, which are pretty nice to work with. If you aren’t familiar with them, please take a look into how they work and how to use them, since pretty often they are what you want for further processing. You can think of them like the “sub” return value of `ind2sub` in MATLAB, but put together as a single meaningful index instead of two distinct values that look like any other integer.
