How to pass vectors as arguments for functions created using str2func ?
Mostrar comentarios más antiguos
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 comentarios
% 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)
el 6 de Feb. de 2018
To add a simple logic: you have defined x but not y and z as inputs ...
Abhishek Panchal
el 6 de Feb. de 2018
Editada: Abhishek Panchal
el 6 de Feb. de 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
el 6 de Feb. de 2018
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Parallel Computing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!