When use || and | in if?
Mostrar comentarios más antiguos
Hello,
when can I use and | in command if?
For example:
if a||b
if a| b
Thank you
Respuesta aceptada
Más respuestas (1)
dpb
el 26 de Abr. de 2014
Depends entirely on the purpose...the double logical operators short-circuit and return only a scalar whereas the single ones are point-by-point operators over the full dimension of the two operands and return a matrix of the same size.
doc relop
has further details and info
2 comentarios
john
el 26 de Abr. de 2014
@John: When a and b are scalars, both versions are equivalent. But the first one || is slightly faster (nano-seconds for scalar operands...), when the first expression is true already. When a and/or b is a vector, you need the , which is equivalent to |or(a==3, b==2). But then the vector expression in the if command is tricky, because implicitly this is performed:
expr = or(a==3, b==2);
if all(expr) && ~isempty(expr) ...
This is at least confusing or can even be a bug, if this behavior is not intended.
Categorías
Más información sobre Data Types en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!