Argmax() -> vector of index result

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).


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.

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

argmax.(eachrow(rand(100,100)))
2 Likes

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

1 Like

@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:

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

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

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.