how to initialize a variable with zero value ?

7 ビュー (過去 30 日間)
yogesh jain
yogesh jain 2016 年 2 月 13 日
編集済み: John D'Errico 2016 年 2 月 13 日
Hello all, I have one function which depends on two variables but it should not depend on the second variable until I change that variable. For example if two variables are x and y(x+y) then for functioning it should depend on x initially not on y. what should I do ? should I initialize y with zero value ? How to do that ?

回答 (1 件)

John D'Errico
John D'Errico 2016 年 2 月 13 日
編集済み: John D'Errico 2016 年 2 月 13 日
Of course it still depends on y! It is just that y was zero in the example that you just set.
Perhaps you wish to set a default value for y, so that if y is not provided, then it uses zero? Learn to use nargin to test if a variable was not provided in a function call.
help nargin
For example...
function z = f(x,y)
if nargin < 2
y = 0;
end
z = y.*(x+y);
end

カテゴリ

Help Center および File ExchangeText Analytics Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by