How to pass vectors as arguments for functions created using str2func ?
古いコメントを表示
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 件のコメント
% 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)
2018 年 2 月 6 日
To add a simple logic: you have defined x but not y and z as inputs ...
Abhishek Panchal
2018 年 2 月 6 日
編集済み: Abhishek Panchal
2018 年 2 月 6 日
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
2018 年 2 月 6 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!