[R] Finding an index of matrix[i,j]

kjetil halvorsen kjetilh at umsanet.edu.bo
Mon Oct 29 17:31:15 CET 2001


> m <- rbinom(25,5,0.5)
> m <- matrix(m,5,5)
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    3    2    4
[2,]    1    4    4    2    1
[3,]    4    3    3    2    3
[4,]    3    2    2    2    1
[5,]    2    2    3    2    2
> max(m)
[1] 4
> ?which
> which(m==max(m), arr.ind=TRUE)
     row col
[1,]   3   1
[2,]   1   2
[3,]   2   2
[4,]   2   3
[5,]   1   5


Kjetil Halvorsen

Wolfgang Viechtbauer wrote:
> 
> I hope somebody can help me out with this. Let's say I have a matrix with
> i rows and j columns and I want to find the index of the row AND column of
> the maximum value in the matrix. I tried to play around with the which()
> command, but couldn't find an easy solution that didn't involve loops and
> various other inelegant approaches. I hope that there is an easy way of
> doing this. Thanks in advance for any suggestions!
> 
> ------------------------------------------------------------------------
> Wolfgang Viechtbauer                       wviechtb at s.psych.uiuc.edu
> Department of Quantitative Psychology      viechtba at students.uiuc.edu
> 
> University of Illinois, Champaign-Urbana   [Virus  based  on  an honors-
> Room 409, Psychology Building              system:  Forward  this  to 10
> 603 E. Daniel Street                       random  people  and  delete a
> Champaign, IL 61820                        bunch of your files. Thanks.]
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list