MATLAB: “If x is between v(h) and v(h+1)”, without knowing which of v(h) or v(h+1) is bigger

compare valuesif statementMATLAB

I want to write an if statement with the variable, x, between two values. However, since it is referencing from an array, there is no guarantee which value will be larger and which will be smaller, therefore I cannot use greater equal or lesser equal statement. So if I want to write "If x is between v(h) and v(h+1)", with no clue of which one will be larger, how should I phrase my code?

Best Answer

if x > min([v(h), v(h+1)]) && x < max([v(h), v(h+1)])
% It's in range.....do stuff....
Related Question