Grokbase Groups R r-help March 2002
FAQ
Dear R-users,

I'm trying to extract the exact location of the maximum value in a
vector or a matrix, meaning I want not only the maximum itself but also
at which position (e.g row 6, column 12) it is located.
I appreciate any suggestions.
Thanks,

Patrick
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patrick.vcf
Type: text/x-vcard
Size: 339 bytes
Desc: Card for Patrick Buetzberger
Url : https://stat.ethz.ch/pipermail/r-help/attachments/20020305/e8b71fd0/patrick.vcf

Search Discussions

  • Peter Dalgaard BSA at Mar 5, 2002 at 3:00 pm

    Patrick Buetzberger <patrick@giub.unibe.ch> writes:

    Dear R-users,

    I'm trying to extract the exact location of the maximum value in a
    vector or a matrix, meaning I want not only the maximum itself but also
    at which position (e.g row 6, column 12) it is located.
    I appreciate any suggestions.
    To find which element of x is equal to the maximum of x, try

    which(x==max(x))

    if you want the array index of the maximum of a matrix, use

    which(m==max(m), arr.ind=TRUE)

    --
    O__ ---- Peter Dalgaard Blegdamsvej 3
    c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
    (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
    ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Laurent Gautier at Mar 5, 2002 at 6:22 pm
    Dear Patrick,


    What about:



    x <- rnorm(100)
    mx <- max(x)
    exact.location <- which(x == mx)



    Hopin' it helps,



    Laurent




    Laurent Gautier CBS, Building 208, DTU
    PhD. Student D-2800 Lyngby,Denmark
    tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent


    On Tue, 5 Mar 2002, Patrick Buetzberger wrote:

    Dear R-users,

    I'm trying to extract the exact location of the maximum value in a
    vector or a matrix, meaning I want not only the maximum itself but also
    at which position (e.g row 6, column 12) it is located.
    I appreciate any suggestions.
    Thanks,

    Patrick
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Agustin Lobo at Mar 5, 2002 at 6:35 pm

    For a vector:
    max(a1) [1] 16667
    (1:length(a1))[a1==max(a1)]
    [1] 6175
    or
    which.max(a1)
    [1] 6175

    For a matrix:
    a2 <- matrix(1:12,ncol=3)
    a2
    [,1] [,2] [,3]
    [1,] 1 5 9
    [2,] 2 6 10
    [3,] 3 7 11
    [4,] 4 8 12
    max(a2) [1] 12
    which.max(a2) [1] 12
    col(a2)
    [,1] [,2] [,3]
    [1,] 1 2 3
    [2,] 1 2 3
    [3,] 1 2 3
    [4,] 1 2 3
    col(a2)[12] [1] 3
    row(a2)[12]
    [1] 4

    Agus


    Dr. Agustin Lobo
    Instituto de Ciencias de la Tierra (CSIC)
    Lluis Sole Sabaris s/n
    08028 Barcelona SPAIN
    tel 34 93409 5410
    fax 34 93411 0012
    alobo at ija.csic.es

    On Tue, 5 Mar 2002, Patrick Buetzberger wrote:

    Dear R-users,

    I'm trying to extract the exact location of the maximum value in a
    vector or a matrix, meaning I want not only the maximum itself but also
    at which position (e.g row 6, column 12) it is located.
    I appreciate any suggestions.
    Thanks,

    Patrick
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedMar 5, '02 at 2:49p
activeMar 5, '02 at 6:35p
posts4
users4
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase