Compare two inputs based on closeness in value to a variable?

3 visualizaciones (últimos 30 días)
rbme17
rbme17 el 14 de Ag. de 2017
Comentada: rbme17 el 18 de Ag. de 2017
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 comentarios
John BG
John BG el 17 de Ag. de 2017
Editada: John BG el 17 de Ag. de 2017
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
rbme17 el 18 de Ag. de 2017
Thanks for your feedback! You're absolutely right, but this is actually being used in a GUI where I prevent x and y from being equal to each other with error messages and a break statement.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Elementary Math en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by