What is the difference between the &
and &&
logical operators in MATLAB?
MATLAB中&&逻辑运算符的区别是什么?
7 个解决方案
#1
83
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)
单& &是逻辑运算符和运算符。double && &又是一个使用短路行为的逻辑和操作符。短路只是意味着第二个操作数(右侧)仅在第一个操作数(左侧)未完全确定结果时才计算。
A & B (A and B are evaluated)
A & B (A和B被评估)
A && B (B is only evaluated if A is true)
A && B (B仅在A为真时进行评估)
#2
24
&&
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.
||总是采用标量输入和短路。只在if/while语句中使用数组输入和短路。对于赋值,后者不会短路。
See these doc pages for more information.
有关更多信息,请参见这些doc页面。
#3
11
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. - ||是第二个操作数的短路版本,只有在第一个操作数没有完全确定的情况下才会对其进行评估。这些只能操作标量,而不是数组。
#4
8
Both are logical AND operations. The && though, is a "short-circuit" operator. From the MATLAB docs:
两者都是逻辑和操作。然而,这是一个“短路”操作符。从MATLAB文档:
They are short-circuit operators in that they evaluate their second operand only when the result is not fully determined by the first operand.
它们是短路操作符,它们只在第一个操作数没有完全确定结果时才计算第二个操作数。
See more here.
看到更多。
#5
5
Similar to other languages, '&' is a logical bitwise operator, while '&&' is a logical operation.
与其他语言类似,'&'是一个逻辑位操作符,而'&&'是一个逻辑操作。
For example (pardon my syntax).
例如(请原谅我的语法)。
If A = [True True False True] B = False
如果A = [True True False True] B = False。
A & B = [False False False False]
A & B =[假假假假]
..or if B = True A & B = [True True False True]
. .如果B = True A & B = [True True False True]
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)
x = (b ~= 0) && (a/b > 18.5)
Hope that's clear.
希望这是清楚的。
#6
3
&& and || are short circuit operators operating on scalars. & and | always evaluate both operands and operate on arrays.
||是在标量上操作的短路操作符。并且|总是对两个操作数进行评估并对数组进行操作。
#7
2
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...
当在条件语句中构造参数时,一个很好的经验法则是,除非有一个很好的理由不这样做,否则总是使用&&/||格式。有两个原因…
- As others have mentioned, the short-circuiting behavior of &&/|| is similar to most C-like languages. That similarity / familiarity is generally considered a point in its favor.
- 正如其他人所提到的,||的短路行为与大多数c语言类似。这种相似性/熟悉度通常被认为是对它有利的一点。
- Using the && or || forms forces you to write the full code for deciding your intent for vector arguments. When a = [1 0 0 1] and b = [0 1 0 1], is a&b true or false? I can't remember the rules for MATLAB's &, can you? Most people can't. On the other hand, if you use && or ||, you're FORCED to write the code "in full" to resolve the condition.
- 使用&&或||形式强制您编写完整的代码,以决定您的意图是否为向量参数。当a =[1 0 0 1]和b =[0 1 0 1]时,a&b是真还是假?我不记得MATLAB的规则了,你能吗?大多数人做不到。另一方面,如果您使用&&或||,则必须“完全”编写代码以解决此问题。
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.
这样做,而不是依赖于MATLAB在&和|上的解析,导致代码更加冗长,但更安全,更易于维护。
#1
83
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)
单& &是逻辑运算符和运算符。double && &又是一个使用短路行为的逻辑和操作符。短路只是意味着第二个操作数(右侧)仅在第一个操作数(左侧)未完全确定结果时才计算。
A & B (A and B are evaluated)
A & B (A和B被评估)
A && B (B is only evaluated if A is true)
A && B (B仅在A为真时进行评估)
#2
24
&&
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.
||总是采用标量输入和短路。只在if/while语句中使用数组输入和短路。对于赋值,后者不会短路。
See these doc pages for more information.
有关更多信息,请参见这些doc页面。
#3
11
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. - ||是第二个操作数的短路版本,只有在第一个操作数没有完全确定的情况下才会对其进行评估。这些只能操作标量,而不是数组。
#4
8
Both are logical AND operations. The && though, is a "short-circuit" operator. From the MATLAB docs:
两者都是逻辑和操作。然而,这是一个“短路”操作符。从MATLAB文档:
They are short-circuit operators in that they evaluate their second operand only when the result is not fully determined by the first operand.
它们是短路操作符,它们只在第一个操作数没有完全确定结果时才计算第二个操作数。
See more here.
看到更多。
#5
5
Similar to other languages, '&' is a logical bitwise operator, while '&&' is a logical operation.
与其他语言类似,'&'是一个逻辑位操作符,而'&&'是一个逻辑操作。
For example (pardon my syntax).
例如(请原谅我的语法)。
If A = [True True False True] B = False
如果A = [True True False True] B = False。
A & B = [False False False False]
A & B =[假假假假]
..or if B = True A & B = [True True False True]
. .如果B = True A & B = [True True False True]
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)
x = (b ~= 0) && (a/b > 18.5)
Hope that's clear.
希望这是清楚的。
#6
3
&& and || are short circuit operators operating on scalars. & and | always evaluate both operands and operate on arrays.
||是在标量上操作的短路操作符。并且|总是对两个操作数进行评估并对数组进行操作。
#7
2
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...
当在条件语句中构造参数时,一个很好的经验法则是,除非有一个很好的理由不这样做,否则总是使用&&/||格式。有两个原因…
- As others have mentioned, the short-circuiting behavior of &&/|| is similar to most C-like languages. That similarity / familiarity is generally considered a point in its favor.
- 正如其他人所提到的,||的短路行为与大多数c语言类似。这种相似性/熟悉度通常被认为是对它有利的一点。
- Using the && or || forms forces you to write the full code for deciding your intent for vector arguments. When a = [1 0 0 1] and b = [0 1 0 1], is a&b true or false? I can't remember the rules for MATLAB's &, can you? Most people can't. On the other hand, if you use && or ||, you're FORCED to write the code "in full" to resolve the condition.
- 使用&&或||形式强制您编写完整的代码,以决定您的意图是否为向量参数。当a =[1 0 0 1]和b =[0 1 0 1]时,a&b是真还是假?我不记得MATLAB的规则了,你能吗?大多数人做不到。另一方面,如果您使用&&或||,则必须“完全”编写代码以解决此问题。
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.
这样做,而不是依赖于MATLAB在&和|上的解析,导致代码更加冗长,但更安全,更易于维护。