Reabo
Reabo

Reputation: 87

Finding the position of maximum in a Matrix in Julia

I want to find the row and the column of the maximum of a matrix.

Let's say A=[1 20 2;30 400 4;4 50 10].

Calling indmax(A) gives 5, but I want to get (2,2).

How can I do that?

Upvotes: 0

Views: 1753

Answers (1)

carstenbauer
carstenbauer

Reputation: 10127

Let me basically just repeat what others have mentioned in the comments.

You can use argmax to obtain the cartesian position of the maximum in your array.

julia> A=[1 20 2;30 400 4;4 50 10];

julia> argmax(A)
CartesianIndex(2, 2)

If you really need the Tuple (2,2), and you probably do not, you can convert the CartesianIndex:

julia> convert(Tuple, argmax(A))
(2, 2)

Upvotes: 2

Related Questions