和 之间有什么区别?和&&在 MATLAB 中?
MATLAB 中的 &
和 &&
逻辑运算符有什么区别?
What is the difference between the &
and &&
logical operators in MATLAB?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
MATLAB 中的 &
和 &&
逻辑运算符有什么区别?
What is the difference between the &
and &&
logical operators in MATLAB?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
单个 & 符号 &是逻辑与运算符。双&&又是一个采用短路行为的逻辑 AND 运算符。 确定时才计算第二个操作数(右侧)。
短路仅意味着仅当结果未完全由第一个操作数(左侧) A & B(评估A和B)
A && B(仅当 A 为真时才评估 B)
The single ampersand & is the logical AND operator. The double ampersand && is again a logical AND operator that employs short-circuiting behaviour. Short-circuiting just means the second operand (right hand side) is evaluated only when the result is not fully determined by the first operand (left hand side)
A & B (A and B are evaluated)
A && B (B is only evaluated if A is true)
&&
和||
采用标量输入并始终短路。|
和&
仅在 if/while 语句中接受数组输入和短路。对于分配,后者不要短路。有关详细信息,请参阅这些文档页面。
&&
and||
take scalar inputs and short-circuit always.|
and&
take array inputs and short-circuit only in if/while statements. For assignment, the latter do not short-circuit.See these doc pages for more information.
正如其他人已经提到的,
&
是一个 逻辑 AND运算符 和&&
是 短路 AND 运算符。它们的不同之处在于操作数的求值方式以及是否对数组或标量进行操作:&
(AND 运算符)和|
(OR 运算符)可以按元素方式对数组进行操作。&&
和||
是短路版本,仅当结果未完全由第一个操作数确定时才计算第二个操作数。它们只能对标量进行操作,而不能对数组进行操作。As already mentioned by others,
&
is a logical AND operator and&&
is a short-circuit AND operator. They differ in how the operands are evaluated as well as whether or not they operate on arrays or scalars:&
(AND operator) and|
(OR operator) can operate on arrays in an element-wise fashion.&&
and||
are short-circuit versions for which the second operand is evaluated only when the result is not fully determined by the first operand. These can only operate on scalars, not arrays.两者都是逻辑与运算。 &&不过,是一个“短路”运算符。来自 MATLAB 文档:
请参阅此处了解更多信息。
Both are logical AND operations. The && though, is a "short-circuit" operator. From the MATLAB docs:
See more here.
&
是逻辑元素运算符,而&&
是逻辑短路运算符(只能对标量进行操作)。例如(请原谅我的语法)。
If..
..or..
对于
&&
,仅当左操作数为 true 时才计算右操作数,并且结果是单个布尔值。x = (b ~= 0) && (a/b > 18.5)
希望这是清楚的。
&
is a logical elementwise operator, while&&
is a logical short-circuiting operator (which can only operate on scalars).For example (pardon my syntax).
If..
..or..
For
&&
, the right operand is only calculated if the left operand is true, and the result is a single boolean value.x = (b ~= 0) && (a/b > 18.5)
Hope that's clear.
&&和||是对标量进行操作的短路运算符。 &和|对数组进行操作,并且仅在以下情况下使用短路
if
或while
循环表达式。&& and || are short circuit operators operating on scalars. & and | operate on arrays, and use short-circuiting only in the context of
if
orwhile
loop expressions.构造用于条件语句(IF、WHILE 等)的参数时,一个好的经验法则是始终使用 &&/||表格,除非有充分的理由不这样做。有两个原因......
这样做,而不是依赖 MATLAB 对 & 中向量的解析。和 | 会导致代码稍微冗长一些,但更安全且更易于维护。
A good rule of thumb when constructing arguments for use in conditional statements (IF, WHILE, etc.) is to always use the &&/|| forms, unless there's a very good reason not to. There are two reasons...
Doing this, rather than relying on MATLAB's resolution of vectors in & and |, leads to code that's a little bit more verbose, but a LOT safer and easier to maintain.