Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Anonymous, recursive, functions

Subject: Anonymous, recursive, functions

From: Jeff

Date: 23 Nov, 2009 04:27:00

Message: 1 of 3

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

Subject: Anonymous, recursive, functions

From: Bruno Luong

Date: 23 Nov, 2009 08:21:01

Message: 2 of 3

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hed2uk$2rv$1@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

Subject: Anonymous, recursive, functions

From: Loren Shure

Date: 2 Dec, 2009 20:48:16

Message: 3 of 3

In article <hedgld$4gj$1@fred.mathworks.com>,
b.luong@fogale.findmycountry says...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hed2uk$2rv$1@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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us