是否有可能在MATLAB中有一个递归匿名函数? [英] Is it possible to have a recursive anonymous function in MATLAB?

查看:330
本文介绍了是否有可能在MATLAB中有一个递归匿名函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一再想要应用一个函数,使用过去的输出作为新的输入。为了便于阅读(我从数学的角度而不是程序员的角度),我想将其定义为一个简单的匿名函数,而不是一个完整的功能块。所以,而不是类似于

pre $ 函数f = myfun(x,n)
如果n> 1
F = myfun(myfun(X,N-1),1);
else
f =表达式(x);
end
end

我希望能写

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

有没有办法实现这一点?

在MATLAB中有一个递归匿名函数的唯一方法是将函数句柄传递给本身作为输入。你可以在匿名函数中调用它。

 %//第一个输入f将是一个匿名函数
myfun = @(f,x,n)f(f,x,n + 1);

%//然后使用你的匿名函数(注意第一个输入)
out = myfun(myfun,x,n);

这显然会导致无限递归,因为您没有任何分支逻辑。如果你想模拟分支逻辑,你需要实现另一个匿名函数来做到这一点( iif 函数借用这里):

 %//匿名函数模拟if / elseif / else 
iif = @(varargin)varargin {2 * find([varargin {1 :2:end}],1,'first')}();

%//你的匿名函数是递归的并且有分支
myfun = @(f,x,n)iif(n> 1,...%if n> 1
@()f(f,f(f,x,n-1),1),...%Recurse
true,...%else
@()expression X)); %执行表达式()

真的有一个,其中包括:通过匿名函数进行这种函数式编程。



小心谨慎



虽然这是一个有趣的练习,我绝对不会推荐使用这个,如果你想让任何人轻松理解你的代码。调试标准功能更加清晰和容易。然后如果你确实需要一个匿名函数,用匿名函数把这个函数调用包装起来。

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

或者只是为函数创建一个函数句柄

  myfunchandle = @myfunc; 


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?

解决方案

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;

这篇关于是否有可能在MATLAB中有一个递归匿名函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆