Position of the max value in a R matrix

By | October 24, 2013

How can one find the position of the max value in a matrix in R? Finding the maximum value of a matrix isn't as straight forward as you may think. We can construct a matrix and find the max value:

mat = max(mat)

But we need to get the position of that max value, 11. To do so, we use the powerful which function as follows:

mat w = which(mat == max(mat), arr.ind = TRUE)
row = w[1]
col = w[2]

How does it work?

We ask which to tell us the location of max(mat), and we ask for the array indices to be returned, which is why we include arr.ind = TRUE. The row and column of the maximum value are now stored in row and col, respectively.

Share
www.pdf24.org    Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *


+ five = 7