[R] indices of maximum in matrix [was: R-help]

Tobias Verbeke tobias.verbeke at telenet.be
Wed Aug 25 11:33:08 CEST 2004


On Wed, 25 Aug 2004 11:07:24 +0200
Jim Gustafsson <jgu at Codan.DK> wrote:

> 
> Dear R users,
> 
> I have just start working with R and would need some help.
> 
> If you have a matrix as:
> 
> 
>        [,1]    [,2]   [,3]
> [1,]   11   24   11
> [2,]   16   29   16
> [3,]    2    15     2
> 
> and you want the position where you can find the maximum value, in this 
> case row 2 and column 2.
> How could you get the position?


> mat <- matrix(c(11,16,2,24,29,15,11,16,2), ncol = 3)
> mat
     [,1] [,2] [,3]
[1,]   11   24   11
[2,]   16   29   16
[3,]    2   15    2
> which(mat == max(mat), arr.ind = TRUE)
     row col
[1,]   2   2


 
> The values in the matrix is likelihood function values, and  each row and 
> column represent values from two parameters. So the idea is to seek which 
> parameter values maximise the likelihood and therefore I need boot row and 
> column position.

HTH,
Tobias




More information about the R-help mailing list