# if statement with greater than and less than

213 visualizaciones (últimos 30 días)
Robert Demyanovich el 21 de Jun. de 2021
Respondida: John D'Errico el 22 de Jun. de 2021
The following doesn't seem to be working as intended:
if 0 <= MBPosition <= 2*Delta ;
Even though MBPosition is within the range, the code moves to the "else" statement. What's the correct way to code this?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

DGM el 21 de Jun. de 2021
Editada: DGM el 21 de Jun. de 2021
I was kind of expecting that to work, but I never write conditionals that way. Try
if (MBPosition >= 0) && (MBPosition <= 2*Delta)
% do thing
end
Otherwise, you might want to check the size of MBPosition and Delta. If they are nonscalar, then the result will only return true if all elements are true.
##### 2 comentariosMostrar NingunoOcultar Ninguno
Robert Demyanovich el 22 de Jun. de 2021
Thanks. I seem to be having a hard time trying to figure out 'Or' in Matlab. So if I wanted to change the code above to be or, is the following correct?
if (MBPosition >= 0) | (MBPosition <= 2*Delta)
John D'Errico el 22 de Jun. de 2021
Editada: John D'Errico el 22 de Jun. de 2021
EXCEPT, you want to use the || operator. The difference is || is a short circuited one. Suppose in an or test, the first fragment of a conditional is true? That is suppose you write
A | B
Then IF A is true, then we know the state of B is irrelevant. In fact, it is silly to even test B, because that test may be time consuming. So MATLAB provides short circuited operators, thus
A || B
This is just an or test like the first, but B is ignored if A was true. In the former case with A|B, MATLAB ALWAYS evaluates the status of both A and B wven when B is irrelevant.
In the curcumstance where A was false, then B must still be evaluated. But a good part of the time, the short-circuiting save CPU sycles.
The same thing applies to the & operator. Suppose you were to write
A & B
Now, if A is false, the status of B is irrelevant. But there, MATLAB ALWAY evaluates both A and B. So we have
A && B
where MATLAB is smart enough to short-ciircuit the second test when A is false. Again, part of the time, B must still be evaluated, because MATLAB will not know if the total conditional is true when A is true in the A&&B case.

Iniciar sesión para comentar.

### Más respuestas (1)

John D'Errico el 22 de Jun. de 2021
This is a common shorthand notation in mathematics, thus
A < B < C
where we know that what is really meant is a pair of tests, thus
(A < B) & (B < C)
The problem is, it fails in MATLAB, Why? When MATLAB executes a test, for example
5 < 7
ans = logical
1
the result will be a boolean value, thus 0 or 1. In this case, it is true, so we get a 1. So what happens when we write a chained test, as the common shorthand uses? For example...
5 < 7 < 2
ans = logical
1
Surely, that must be false. But MATLAB thinks it was true. Why? MATLAB works from left to right. It breaks that test down as if you had written:
(5 < 7) < 2
Again now, what is the result of 5<7? That is true, so it is 1. Is 1 < 2? Of course. So MATLAB thinks the chained conditional
(5 < 7 < 2) is actually a TRUE statement!
The point being, you cannot use chained conditions as you do in the common mathematics shorthand notation. Sorry, you cannot do so. You must write the chain as two separate conditions.
(A < B) & (B < C)
Better of course, is you really want to use the short-circuited operators, thus
(A < B) && (B < C)
This way the second fragment in that test will not be evaluated depending on the state of the first.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by