[R] Optimize a function with Discrete inputs

peter dalgaard pdalgd at gmail.com
Wed Aug 1 18:11:39 CEST 2012


On Aug 1, 2012, at 16:34 , Sarah Goslee wrote:

> combn() gives ordered combinations, while expand.grid() gives all combinations.

...and there's one more function that is designed to tabulate a function of two variables over a grid. And yet another one to find which value is max in an array and return the array indices.

> I'd give worked code but this hints at homework to me.
> 

Yes... And since I would expect that most can solve the stated problem in their head, I wonder what the question is, and how it involves R. 

> Sarah
> 
> On Wed, Aug 1, 2012 at 10:23 AM, Eik Vettorazzi <E.Vettorazzi at uke.de> wrote:
>> Hi,
>> not sure if that is what you are looking for, but have a look at
>> 
>> cmb<-t(combn(c(0,3,5,8),2))   #get all pairs of combinations
>> cbind(cmb,apply(cmb,1,diff))  #for each pair, get the difference
>> 
>> cheers
>> 
>> 
>> Am 01.08.2012 12:29, schrieb loyolite270:
>>> Hi
>>> 
>>> I need to optimize the below function:
>>> 
>>> a=function(x){
>>>     A=x[1]
>>>     B=x[2]
>>>     C=B-A
>>> return(C)
>>> }
>>> 
>>> I need to optimize the above function such that x can be any combination of
>>> these number (0,3,5,8) of vector length 2
>>> (i.e) x can be (3,0), (5,0), (8,0), (3,5), (3,8), (5,8), ...... etc
>>> 
>>> can someone please help me solve this problem ?
>>> 
>>> 
>>> 
> 
> 
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org
> 
> ______________________________________________
> R-help at r-project.org 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list