フィルターのクリア

Update a variable within a function and recalculate

2 ビュー (過去 30 日間)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 5 月 22 日
I have created the following function
function y=fun(x)
y=2*x;
if y<0
x=-x
end
and I would like to update the input argument x if x is negative so then it invokes again the function. How can I do that?

採用された回答

Iain
Iain 2013 年 5 月 22 日
once you invert x, add the line...
y = fun(x);
  3 件のコメント
Iain
Iain 2013 年 5 月 22 日
Theres a few things you can do.
I recommend, wherever possible, making sure that the input is correct before even getting to the actual business code. In this case x = abs(x);
Where that isn't possible, you can repeatedly call the function like that. And the last option is to place a while loop around your business code.
answer_invalid = true; keep_trying = true;
while keep_trying & answer_invalid
%business code here:
do some checks on the answer to see if it is valid, and update answer_invalid as needed. do some checks to make sure you haven't gotten stuck in an infinite loop and set keep_trying to false if so.
end
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 5 月 22 日
編集済み: Giorgos Papakonstantinou 2013 年 5 月 22 日

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by