Finding coordinates for maximum of a function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Finding coordinates for maximum of a function

derek
Hello R,

I have data in txt file of n columns like this:
x  y1 .....yn
1  100
2  50
3  10
4  200
5  20


I need to find coordinates for each maxima e.g.: [x,y1],[x,y2]...[x,yn]

a=read.table()
attach(a)
b=y1<-x
max(b) returns only value of y but no corresponding x value

Reply | Threaded
Open this post in threaded view
|

Re: Finding coordinates for maximum of a function

nblarson
This is where which.max() comes in handy.

Let Y be a matrix of all of column vectors of y

n<-length(x)
x.c<-rep(0,n)
for(i in 1:n){
x.c[i]<-which.max(Y[,i])
}

x.c is then a vector of x coordinates for the maximum for columns y1,y2,...,yn
Reply | Threaded
Open this post in threaded view
|

Re: Finding coordinates for maximum of a function

nblarson
This post was updated on .
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Finding coordinates for maximum of a function

derek
It occured to me:
x[max(y)] simple isn't it?
Reply | Threaded
Open this post in threaded view
|

Re: Finding coordinates for maximum of a function

nblarson
That actually won't work.  max(y) will give a value, not a coordinate, so x[max(y)] is definitely not what you want.
Reply | Threaded
Open this post in threaded view
|

Re: Finding coordinates for maximum of a function

Bert Gunter
Right.

?which.max

is what's needed.

-- Bert

On Tue, Mar 15, 2011 at 6:00 AM, nblarson <[hidden email]> wrote:

> That actually won't work.  max(y) will give a value, not a coordinate, so
> x[max(y)] is definitely not what you want.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Finding-coordinates-for-maximum-of-a-function-tp3355369p3356483.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Bert Gunter
Genentech Nonclinical Biostatistics

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Finding coordinates for maximum of a function

derek
I must have mixed it up. Thank you.