和 之间有什么区别?和&&在 MATLAB 中?

发布于 2024-08-03 16:59:12 字数 73 浏览 5 评论 0原文

MATLAB 中的 &&& 逻辑运算符有什么区别?

What is the difference between the & and && logical operators in MATLAB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浮生面具三千个 2024-08-10 16:59:12

单个 & 符号 &是逻辑与运算符。双&&又是一个采用短路行为的逻辑 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)

放肆 2024-08-10 16:59:12

&&|| 采用标量输入并始终短路。 |& 仅在 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.

离笑几人歌 2024-08-10 16:59:12

正如其他人已经提到的, & 是一个 逻辑 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.
最单纯的乌龟 2024-08-10 16:59:12

两者都是逻辑与运算。 &&不过,是一个“短路”运算符。来自 MATLAB 文档:

它们是短路运算符,因为只有当结果未完全由第一个操作数确定时,它们才会计算第二个操作数。

请参阅此处了解更多信息。

Both are logical AND operations. The && though, is a "short-circuit" operator. From the MATLAB docs:

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.

我恋#小黄人 2024-08-10 16:59:12

& 是逻辑元素运算符,而 && 是逻辑短路运算符(只能对标量进行操作)。

例如(请原谅我的语法)。

If..

A = [True True False True]
B = False
A & B = [False False False False]

..or..

B = True
A & B = [True True False True]

对于 &&,仅当左操作数为 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..

A = [True True False True]
B = False
A & B = [False False False False]

..or..

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)

Hope that's clear.

靖瑶 2024-08-10 16:59:12

&&和||是对标量进行操作的短路运算符。 &和|对数组进行操作,并且仅在以下情况下使用短路 ifwhile 循环表达式。

&& and || are short circuit operators operating on scalars. & and | operate on arrays, and use short-circuiting only in the context of if or while loop expressions.

笑看君怀她人 2024-08-10 16:59:12

构造用于条件语句(IF、WHILE 等)的参数时,一个好的经验法则是始终使用 &&/||表格,除非有充分的理由不这样做。有两个原因......

  1. 正如其他人提到的, &&/|| 的短路行为与大多数类 C 语言类似。这种相似性/熟悉性通常被认为是对其有利的一点。
  2. 使用 &&或||表单强制您编写完整的代码来决定向量参数的意图。当a = [1 0 0 1]且b = [0 1 0 1]时,a&b是真还是假?我不记得 MATLAB 的 & 规则了,你记得吗?大多数人都做不到。另一方面,如果您使用 &&或 ||,您被迫“完整”编写代码来解决该情况。

这样做,而不是依赖 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...

  1. 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.
  2. 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文