Compare two inputs based on closeness in value to a variable?
古いコメントを表示
Hi everyone,
I'm trying to write a statement that compares two inputs to a number. Depending on which of them is closer to that number, a statement will use the closer variable that in a calculation.
So for example, I have two inputs from a GUI.
x = 44, and y = 52
Whichever one is closer to 51 will be used in an equation (i.e. y)
Does anyone know how I can accomplish this?
Thank you!
4 件のコメント
rbme17
2017 年 8 月 14 日
Aveek Podder
2017 年 8 月 17 日
If both x and y are equidistant from 51 then some error may rise:
if true
if abs(x - 51) > abs(y - 51)
z = y;
else
z = x;
end
end
What about solving the equal distance ambiguity with for instance
if abs(x - 51) > abs(y - 51)
z = y;
else
z=x
end
if abs(x - 51) == abs(y - 51)
z = [x y];
end
rbme17
2017 年 8 月 18 日
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!