From: Jeff on 22 Nov 2009 23:27 Can I create an anonymous, recursive, function? Here's the function I have now: function a=HW8Q1(An, i) if i==0 a=0; else a=i*An(An, i-1) + 2*factorial(i); end end But this is too simple to make a whole function from and I don't want to litter my M-file with tons of little functions. I want a simple, anonymous function, like this: An = @(An, i)if i<=0,0,else,i*An(An,i-1)+2*factorial(i),end; It seems I cannot do this (or anything similar). I am told there is a "Parse error at IF", even if I put parenthesis around it. Thanks in advance. -Jeff
From: Bruno Luong on 23 Nov 2009 03:21 "Jeff " <spREMOVEHITSjeffAT(a)SIGNoptonline.net> wrote in message <hed2uk$2rv$1(a)fred.mathworks.com>... > Can I create an anonymous, recursive, function? > > Here's the function I have now: > > function a=HW8Q1(An, i) > if i==0 > a=0; > else > a=i*An(An, i-1) + 2*factorial(i); > end > end > > But this is too simple to make a whole function from and I don't want to litter my M-file with tons of little functions. I want a simple, anonymous function, like this: > > An = @(An, i)if i<=0,0,else,i*An(An,i-1)+2*factorial(i),end; > Anonymous function is restricted to a single statement. So it's not possible, unless using a contortion code to work around. I believe there is a similar thread a year ago. There is nothing wrong with little regular functions. Bruno
From: Loren Shure on 2 Dec 2009 15:48 In article <hedgld$4gj$1(a)fred.mathworks.com>, b.luong(a)fogale.findmycountry says... > "Jeff " <spREMOVEHITSjeffAT(a)SIGNoptonline.net> wrote in message <hed2uk$2rv$1(a)fred.mathworks.com>... > > Can I create an anonymous, recursive, function? > > > > Here's the function I have now: > > > > function a=HW8Q1(An, i) > > if i==0 > > a=0; > > else > > a=i*An(An, i-1) + 2*factorial(i); > > end > > end > > > > But this is too simple to make a whole function from and I don't want to litter my M-file with tons of little functions. I want a simple, anonymous function, like this: > > > > An = @(An, i)if i<=0,0,else,i*An(An,i-1)+2*factorial(i),end; > > > > Anonymous function is restricted to a single statement. So it's not possible, unless using a contortion code to work around. I believe there is a similar thread a year ago. > > There is nothing wrong with little regular functions. > > Bruno > you can achieve some if statement behavior by using arithmetic with logical variables. Here's an example (not yours) f = @(x) (x==1)*1 + (x==0)*3 -- Loren http://blogs.mathworks.com/loren
|
Pages: 1 Prev: Simulink accelerator problem Next: wound rotor induction generator modelling |