Is it possible to have a recursive anonymous function in MATLAB? [duplicate]

核能气质少年 提交于 2019-12-23 16:24:15

问题


I repeatedly want to apply a function, using a past output as the new input. For readability (I'm writing from a mathematics perspective, not a programmer's perspective), I would like to define it as a simple anonymous function instead of a full function block. So, instead of something like

function f=myfun(x,n)
    if n>1
        f=myfun(myfun(x,n-1),1);
    else
        f=expression(x);
    end
end

I would like to be able to write

f=@(x,n) ????

Is there any way this is possible?


回答1:


The only way to have a recursive anonymous function in MATLAB is to pass the function handle to itself as an input. You can then call it from within the anonymous function.

%// The first input f is going to be an anonymous function
myfun = @(f,x,n) f(f, x, n+1);

%// Then use your anonymous function like this (note the first input)
out = myfun(myfun, x, n);

This will obviously result in infinite recursion since you don't have any branching logic. If you want to simulate the branching logic, you would need to implement another anonymous function to do this (iif function borrowed from here):

%// Anonymous function to simulate if/elseif/else
iif = @(varargin) varargin{2*find([varargin{1:2:end}], 1, 'first')}();

%// Your anonymous function that is recursive AND has branching
myfun = @(f,x,n)iif(n > 1, ...                      % if n > 1
                    @()f(f, f(f, x, n-1), 1), ...   % Recurse
                    true, ...                       % else
                    @()expression(x));              % Execute expression()

There is a really solid series of blog entries on the Mathworks site that goes over this sort of functional programming using anonymous functions.

A Word of Caution

While this is an interesting exercise, I definitely wouldn't recommend using this if you want anybody to understand your code easily. It is far clearer and easier to debug a standard function. Then if you really need an anonymous function, wrap the call to that function in an anonymous function.

myanonfunc = @(varargin)myfunc(varargin{:});

Or just create a function handle to the function

myfunchandle = @myfunc;


来源:https://stackoverflow.com/questions/36310676/is-it-possible-to-have-a-recursive-anonymous-function-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!