anonymous function for if-else statements
이전 댓글 표시
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
Jan
2017년 7월 19일
Don't use eval. If you talk about expensive functions, the performance degradation by eval must be important.
답변 (4개)
Walter Roberson
2017년 7월 19일
SelectCell = @(C, idx) C{idx}
condfunc = @(x, scalarcondition, varargin) feval(SelectCell(varargin, scalarcondition+1),x)
Example:
condfunc(x, condition, @b, @c)
댓글 수: 5
Patrick Mboma
2017년 7월 20일
Patrick Mboma
2017년 7월 20일
Walter Roberson
2017년 7월 20일
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
2017년 7월 20일
Sean de Wolski
2017년 7월 19일
2 개 추천
In R2016b and newer you can have subfunctions in scripts as well as functions. I would recommend using them instead.
댓글 수: 4
Patrick Mboma
2017년 7월 19일
Walter Roberson
2017년 7월 20일
This requires R2016b or later. It also requires that the script file be named something different than the name of any of the functions defined in the script.
Patrick Mboma
2017년 7월 20일
Andrei Bobrov
2017년 7월 20일
@Patrick Mboma: yes.
Jan
2017년 7월 19일
@(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
Patrick Mboma
2017년 7월 19일
Walter Roberson
2017년 7월 20일
Are the "generic problems" being dynamically generated?
Can these functions output +/- inf, or nan ?
Patrick Mboma
2017년 7월 20일
Jan
2017년 7월 20일
Writing M-files is fine and in your case it might be more efficient than creating anonymous functions dynamically. Better write a "generic function", which describes the "generic problem" and control the details by input arguments. The simpler, the better.
Patrick Mboma
2017년 7월 20일
Walter Roberson
2017년 7월 20일
I am not sure what "generic form" means in this case...
The reason I ask about nan and inf is that some of the useful methods for conditional computation fail if nan or inf is involved, as they rely upon multiplication by 0 to ignore the computed value that is not wanted; that fails if the value to be ignored is nan or inf because 0*nan and 0*inf are both nan instead of 0.
Patrick Mboma
2017년 7월 21일
Walter Roberson
2017년 7월 21일
I would use the Symbolic Toolbox for the kind of situation you describe.
Patrick Mboma
2017년 7월 21일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!