Effacer les filtres
Effacer les filtres

difference between | and || in my function

73 vues (au cours des 30 derniers jours)
sri satya ravi
sri satya ravi le 11 Jan 2017
Modifié(e) : Stephen23 le 12 Jan 2017
Hi ;
I have a code in which
function metCondition = Ambient_Temperature(vector)
metCondition = true; % Initialize
if any((vector) <= -7 | (vector) >= 37.86) %degC
metCondition = false;
end
what is the difference between
if any((vector) <= -7 | (vector) >= 37.86) %degC
and
if any((vector) <= -7 || (vector) >= 37.86) %degC
and why am i getting an error when using ||.
Thanks

Réponse acceptée

James Tursa
James Tursa le 11 Jan 2017
Modifié(e) : Stephen23 le 12 Jan 2017
The "|" operator is an element-wise operator, intended to be used on arrays element-by-element. The "||" operator is a short-circuiting operator restricted to be used on scalars only. See the doc:
For your case, you clearly want the | operator.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 11 Jan 2017
You could use
if any((vector) <= -7) || any((vector) >= 37.86) %degC
|| can only be used when both sides return scalars. || is the "short circuit" OR operator -- it does not bother evaluating the right hand side of the left hand side is already true.

Catégories

En savoir plus sur Data Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by