[R] returning the largest element in an array/matrix?

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 7 07:48:53 CET 2006


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
>




More information about the R-help mailing list