if statement with greater than and less than
158 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Robert Demyanovich
il 21 Giu 2021
Risposto: John D'Errico
il 22 Giu 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 Commenti
Risposta accettata
DGM
il 21 Giu 2021
Modificato: DGM
il 21 Giu 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 Commenti
John D'Errico
il 22 Giu 2021
Modificato: John D'Errico
il 22 Giu 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.
Più risposte (1)
John D'Errico
il 22 Giu 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
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
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 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!