anonymous function for if-else statements

41 visualizzazioni (ultimi 30 giorni)
Patrick Mboma
Patrick Mboma il 19 Lug 2017
Commentato: Patrick Mboma il 21 Lug 2017
Hi, Is it possible to write an anonymous function or a function handle that replicates the behavior of if-else statements?
Consider the simple problem
if condition
a=b(x);
else
a=c(x);
end
it is possible to write the following function that will replicate that behavior
function out=ifelse(condition,answer1,answer2)
if condition
out=answer1;
else
out=answer2;
end
A critical difference between the first and the second pieces of code is the fact that in the second one, both answer1 and answer 2 need to be computed/evaluated before passing them to the ifelse function. For small problems this is not really a problem. However, in a situation where b(x) or c(x) are expensive to compute, it is best to avoid un-necessary operations.
One workaround would be
function out=ifelse2(condition,input1,input2)
if condition
out=eval(input1);
else
out=eval(input2);
end
In this case though we have to use "eval".
Is there any other way to deal with this, possibly using some kind of anonymous function or a function handle?
Thanks
  1 Commento
Jan
Jan il 19 Lug 2017
Don't use eval. If you talk about expensive functions, the performance degradation by eval must be important.

Accedi per commentare.

Risposte (4)

Walter Roberson
Walter Roberson il 19 Lug 2017
SelectCell = @(C, idx) C{idx}
condfunc = @(x, scalarcondition, varargin) feval(SelectCell(varargin, scalarcondition+1),x)
Example:
condfunc(x, condition, @b, @c)
  5 Commenti
Walter Roberson
Walter Roberson il 20 Lug 2017
When the scalarcondition is logical, then false corresponds to 0 and true corresponds to 1, and adding one to that takes it into the range 1 or 2, which is suitable for indexing a two element array. However, you might want to use 2-scalarcondition instead, to reverse the order of the tests (the first one corresponds to false in the way I wrote the code.)
Patrick Mboma
Patrick Mboma il 20 Lug 2017
My bad, I missed that possibility. I thought scalarcondition was 1 or 2. Thanks Walter.

Accedi per commentare.


Sean de Wolski
Sean de Wolski il 19 Lug 2017
In R2016b and newer you can have subfunctions in scripts as well as functions. I would recommend using them instead.
  4 Commenti
Patrick Mboma
Patrick Mboma il 20 Lug 2017
So, the script has to be saved?
Andrei Bobrov
Andrei Bobrov il 20 Lug 2017
@Patrick Mboma: yes.

Accedi per commentare.


Adam
Adam il 20 Lug 2017
  1 Commento
Patrick Mboma
Patrick Mboma il 20 Lug 2017
Hi Adam,
Thanks for your input. Functional programming, although I am not expert in it, in large part inspires some of the things I do here. For instance the iff construct that is discussed in part-1 is similar to my ifelse function above and shares the same drawback that all alternatives have to be evaluated before the choice is made, which can be very costly.

Accedi per commentare.


Jan
Jan il 19 Lug 2017
@(x) condition * b(x) + (~condition) * c(x)
But you see, that b(x) and c(x) are evaluated also. The best strategy is not to use anonymous functions, but normal functions. Then you have the full power and the best speed.
  9 Commenti
Walter Roberson
Walter Roberson il 21 Lug 2017
I would use the Symbolic Toolbox for the kind of situation you describe.
Patrick Mboma
Patrick Mboma il 21 Lug 2017
Hi Walter,
The point is not what tools you use, but rather how you handle the output. The analogy I made above was just to make a point. Essentially, some people use my codes to solve a problem I don't know anything about. But for my codes to solve the problem, my procedures will first generate some functions that are specific to the problem at hand. Those functions are the ones I want to avoid writing to an m-file.

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by