Hi all, I want to use "which.max" to identify the maximum in a 2D array/matrix and I want "argmin" and return the row and column indices. But "which.max" only works for vector... Is there any convinient way to solve this problem? Thanks a lot! [[alternative HTML version deleted]]
Jacques VESLOT
2006-Mar-07 05:09 UTC
[R] returning the largest element in an array/matrix?
indcol <- rep(1:ncol(mat), each=nrow(mat))[which.max(mat)] indrow <- rep(1:nrow(mat), ncol(mat))[which.max(mat)] indrow <- which(apply(mat==max(mat),1,sum)!=0) indcol <- which(apply(mat==max(mat),2,sum)!=0) Michael a ?crit :>Hi all, > >I want to use "which.max" to identify the maximum in a 2D array/matrix and I >want "argmin" and return the row and column indices. > >But "which.max" only works for vector... > >Is there any convinient way to solve this problem? > >Thanks a lot! > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >
Gabor Grothendieck
2006-Mar-07 06:48 UTC
[R] returning the largest element in an array/matrix?
Try this: m <- diag(4-abs(-4:4)) # test matrix wm <- which.max(m) c(row(m)[wm], col(m)[wm]) # c(5,5) On 3/6/06, Michael <comtech.usa at gmail.com> wrote:> Hi all, > > I want to use "which.max" to identify the maximum in a 2D array/matrix and I > want "argmin" and return the row and column indices. > > But "which.max" only works for vector... > > Is there any convinient way to solve this problem? > > Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Hi If you do not insist on which.max() you can use which(mat==max(mat), arr.ind=T) HTH Petr On 6 Mar 2006 at 20:55, Michael wrote: Date sent: Mon, 6 Mar 2006 20:55:20 -0800 From: Michael <comtech.usa at gmail.com> To: R-help at stat.math.ethz.ch Subject: [R] returning the largest element in an array/matrix?> Hi all, > > I want to use "which.max" to identify the maximum in a 2D array/matrix > and I want "argmin" and return the row and column indices. > > But "which.max" only works for vector... > > Is there any convinient way to solve this problem? > > Thanks a lot! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz