[R] Optimize a function with Discrete inputs

Sarah Goslee sarah.goslee at gmail.com
Wed Aug 1 16:34:01 CEST 2012


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

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

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



More information about the R-help mailing list