generalize a function according to input
5 次查看(过去 30 天)
显示 更早的评论
Hi, i need your suggestion about generalize a code for each variable. For example, i have a code, but it is special for x. I have a function like f(x)=x.^2-3*x+5; and this code generate f(x) according to x. I want to generalize code for x,y,z,... and more variable, for example if my function is f(x,y)= x.^2-2*y+6, i want code to work for x and y variable.
So i need some beginning suggestions, i'm in searching process. Thank you.
3 个评论
采纳的回答
hcai
2016-8-16
编辑:hcai
2016-8-16
You can try using varargin, which allows functions to take a variable number of inputs.
function result = f(varargin)
Inside your function, use nargin or the length of the varargin cell array to determine the number of function inputs. From there, use if statements to specify what do to for each case.
if nargin == 1
x = varargin{1};
x.^2-3*x+5;
elseif nargin == 2
...
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!