[R] Re: [S] First max of a vector

Martin Maechler maechler at stat.math.ethz.ch
Mon Sep 9 10:01:04 CEST 2002


>>>>> "MikeM" == Michael M Meyer <mikem at salter-point.com>
>>>>>     on Sat, 07 Sep 2002 18:40:01 -0700 writes:

    MikeM> which(x==max(x)) will return the indices fo all
    MikeM> elements that are equal to the max.  So the first max
    MikeM> would be which(x==max(x))[1] (At least this works in
    MikeM> R, I assume it does in S-plus....?)  --Mike

yes, (of course).

However, the above solution is quite sub-optimal at least in
principle:
  1) run through all of x finding the *value* of the max()
  2) run through all of x comparing x[i] with the max(), returning
     a logical vector of TRUE/FALSE
  3) extract the TRUE indices

For this reason, and because it's such a common task, I had
introduced the functions

	   which.min()   and   which.max()

into R, a while ago [R 1.1.0 (June 15, 2000)].
Speed comparison when x is of length 10000,
shows a speedup of about a factor of 13 {varying between 9 & 20}.

S-plus is not yet compatible here.. :-) ;-)

    >> ***> Abdulkadir Hussein wrote (Fri, 06 Sep 2002 17:55:33
    >> MDT) Hi S-group, Is anyone aware of a function in S-plus
    >> which returns the index (position) of the first global
    >> max of a vector x? ... I would appreciate any
    >> suggestions..
    >> 
    >> Regards Abdul
    >> __________________________________________________________________
    >> Abdulkadir Ahmed Hussein Department of Mathematical
    >> Sciences University of Alberta Edmonton, Alberta Canada
    >> T6G 2G1 Office: CAB 664 Phone: (403)436-4943 W3:
    >> http://www.stat.ualberta.ca/~hussein
    >> ___________________________________________________________________

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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