How to pass vectors as arguments for functions created using str2func ?
Afficher commentaires plus anciens
f=str2func('@(x,y,z) x+y+z');
w=[1,1,1];
y=f(w) % want to compute f(1,1,1) using vector x
This gives an error when I tried to compute y
Thanks in Advance for your help !!
5 commentaires
% want to compute f(1,1,1) using vector x
The problem is that f(1,1,1) is NOT one vector, but three independent input arguments to f. Confusion about very basic MATLAB concepts like this is best helped by doing the introductory tutorials (which are highly recommended for all beginners):
And in particular this section:
Jos (10584)
le 6 Fév 2018
To add a simple logic: you have defined x but not y and z as inputs ...
Abhishek Panchal
le 6 Fév 2018
Modifié(e) : Abhishek Panchal
le 6 Fév 2018
f= @(w) sum(w);
w=[1,1,1];
y=f(w)
would do what you ask. str2func just obfuscates what you are doing by adding an extra un-needed layer.
If you want to do something more complicated than a sum on a vector input though that is complicated in an anonymous function like that given variable input lengths.
Abhishek Panchal
le 6 Fév 2018
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Parallel Computing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!