Logical short-circuit inside a function handle

你。 提交于 2019-11-29 13:53:19
knedlsepp

As already mentioned in the comments: Doing this inline is madness and you are much better off using a separate function / .m file.

It will be

  • Faster
  • Easier to read
  • Easier to write
  • Easier to debug

You could do this for example in a similar way to this:

function out = R2TComputation(DL1, DL2, minLim, maxLim, C1)
...%Compute whatever R2T would compute.

To get the same interface as your original anonymous function has, you can simply create

R2T = @(DL1, DL2) R2TComputation(DL1, DL2, minLim, maxLim, C1)

which will capture the current values of minLim, maxLim and C1 at the time you create this handle R2T.


Yet another option would be to use a nested function instead of the external one. It would have access to the parent function's variables, yet still be able to use if, else and all the other basic tools you need. Only downside: It is not meant to be accessed from within other files.

... % Main function stuff
     function out = R2T(DL1, DL2)
         if ...
            out = ...
         ...
     end
... % Use R2T ...

However, for the sake of freedom of shooting oneself in the foot, here is an inline version of if-else, which I wrote in the spirit of Loren's blog post and I do not recommend using, as there are hardly any benefits of using a single expression instead of the corresponding if-else statements.

ifelse = @(cond, varargin) varargin{1+~cond}(); %Only for the insane

If you want it to do lazy evaluation, you need to pass an anonymous function with zero parameters, which ifelse will then evaluate (That's what the last two parentheses () in ifelse are for):

ifelse(true, 42, @()disp('OMG! WTF! THIS IS CRAZY!!111'))

If you simply wrote the function call to disp as an argument to ifelse without @(), the function would be called before we even access ifelse. This is because MATLAB (as most other languages) first computes the return value of the function, which is then passed to ifelse as a parameter.

In your case the resulting code would be:

R2T = @(DL1,DL2) arrayfun(@(DL1,DL2)...
                 ifelse(~any(isnan([DL1, DL2])), ...
                        @() 1/(fzero(@(x)fFitObj1(x)./fFitObj2(x)-DL1./DL2,LIM)), ...
                        NaN), ...
                 DL1, DL2) - C1;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!