Instead of the following, I want to use function handles and move if-statments out of the loop:
for n=1:10
if a==0
A = function2(n,a,b);
else
A = function1(n,a,b,c,d);
end
end
function A = function1(n,a,b,c,d)
A = n+a+b+c+d; % here it can be any expression
end
function A = function2(n,a,b)
A = n*a*b; % here it can be any expression
end
moving if-statement outside of the loop and using handles:
if a==0
func = @function2;
else
func = @function1;
end
and then I can solve my problem in two ways as
for n=1:10
A = func(n,a,b,c,d);
end
function A = function1(n,a,b,c,d)
A = n+a+b+c+d;
end
function A = function2(n,a,b,~,~)
A = n*a*b;
end
or as
for n=1:10
A = func(n,a,b,c,d)
end
function A = function1(n,a,varargin)
b = varargin{1};
c = varargin{2};
d = varargin{3};
A = n+a+b+c+d;
end
function A = function2(n,a,varargin)
b = varargin{1};
A = n*a*b;
end
Is there any more elegant way to do this?

1 commentaire

G A
G A le 29 Août 2021
may be this way could be better:
function A = function1(n,S)
A = n + S.a + S.b + S.c + S.d;
end
function A = function2(n,S)
A = n*S.a*S.b;
end

Connectez-vous pour commenter.

 Réponse acceptée

Yongjian Feng
Yongjian Feng le 29 Août 2021

0 votes

You don't need varargin
function A = function1(n,a,b, c, d)
A = n+a+b+c+d;
end
function A = function2(n,a,b, ~, ~)
A = n*a*b;
end

7 commentaires

G A
G A le 29 Août 2021
Modifié(e) : G A le 29 Août 2021
That is the one way - without varargin. My question was - is any way better than this, i.e. without matching the number of arguments?
Stephen23
Stephen23 le 30 Août 2021
This is the best approach. In contrast when you use VARARGIN you lose the clarity of this function signature, which:
  • shows the variable names in MATLAB Editor (e.g. tab completion / pop-up help)
  • clearly indicates which variables are not used within the function
G A
G A le 30 Août 2021
Stephen, what about the approach with using struct (see my other comment)?
Stephen23
Stephen23 le 30 Août 2021
Modifié(e) : Stephen23 le 30 Août 2021
@G A: using a structure is also good approach. Which approach to use depends on many things, you will have to find the right balance/optimum for your project and needs, e.g.:
structure:
  • useful when you have many arrays (e.g. more than 6 or 7) which easily lead to confusion when supplied as positional input arguments (we regularly get questions on this topic).
  • might be easier to process when those arrays are optional
input arguments:
  • (partially) self-documentating names
  • pop-up help in the Editor, etc.
I am sure that other users on this forum can add other benefits or considerations.
G A
G A le 30 Août 2021
Thanks, Stephen! One question: what is the main difficulty to make (or reason not to make) it possible in the Editor to higlight structure name with its field throughout a function like it occurs with simple variables when you click on it or select it? Is this question to go to "What frustrates you about Matlab"?
Stephen23
Stephen23 le 30 Août 2021
"what is the main difficulty to make (or reason not to make) it possible in the Editor to higlight structure name with its field throughout a function like it occurs with simple variables when you click on it or select it?"
You have to consider that variables in MATLAB are dynamically typed. In general it is difficult to determine the type of a variable until runtime (e.g. changes to function scoping, variables created dynamically, inputs are not typed, etc.)
G A
G A le 31 Août 2021
Thanks! - and I will accept this answer together with comments.

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 29 Août 2021
I think the basic idea you need is
f1 = @(x) x;
f2 = @(x) x.^2;
f = @(a,x) (a~=0)*f1(x) + (a==0)*f2(x);
f(0,2)
ans = 4
f(1,2)
ans = 2

4 commentaires

G A
G A le 29 Août 2021
Modifié(e) : G A le 29 Août 2021
How would you apply your idea to the non-anonimous functions with different number of input arguments which must be passed within for-loop? The other question is: when a = 0, will Matlab execute function f2 to multiply its return by 0 or, seeing that one term is 0, it will return 0 without execution of f2?
Hm. Not finding a good way to handle this that does not potentially result in empty, Inf, or NaN when one of the functions does not have all the arguments. For example, you could pass zeros into the function when they are not needed, but it is potentially hazardous:
f1 = @(x) x;
f2 = @(x,y) x.^2 + y.^2;
f = @(a,x,y) (a~=0)*f1(x) + (a==0)*f2(x,y);
f(0,2,3)
ans = 13
f(1,2,0)
ans = 2
G A
G A le 29 Août 2021
Modifié(e) : G A le 29 Août 2021
Also, it is time consuming if two functions are executed instead of one. Better to put if-statement into a loop. I have half of answer to my question in my comment above: 0*inf = NaN, 0*A = 0, so to give the answer 0 or NaN, Matlab must execute f2 before multipliing its return by 0.
the cyclist
the cyclist le 29 Août 2021
Searching keywords such as conditional anonymous function MATLAB turns up a lot of these same ideas. I did not find a great solution.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Centre d'aide et File Exchange

Produits

Question posée :

G A
le 29 Août 2021

Commenté :

G A
le 31 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by