【问题标题】:Operands to the && operators must be convertible to logical scalar values:&& 运算符的操作数必须可转换为逻辑标量值:
【发布时间】:2023-04-07 08:22:01
【问题描述】:

这行代码有什么错误:

 if BB(i) <= max([BB(i-4):BB(i)]) && BB(i) <= max([BB(i-7):BB(i)])

我收到以下错误消息:

??? Operands to the || and && operators must be convertible to logical scalar values.
Error in ==> if BB(i) <= max([BB(i-4):BB(i)]) && BB(i) <= max([BB(i-7):BB(i)])

【问题讨论】:

  • 你有更多关于BB 的信息吗?另外我猜你的意思是max(BB((i-4):i))
  • @Suever:BB 是一个向量。你说得对,我的意思是 max(BB((i-4):i))。谢谢。

标签: matlab logical-operators operands


【解决方案1】:

尝试更改为&amp;
&amp;&amp;|| 是使用短路 行为的简写,请参阅herehere

【讨论】:

  • 看来 OP 确实想要 &amp;&amp; 这里。事实上,使用&amp;| 可能会隐藏潜在的错误,当旨在返回标量的测试(如在OP 的问题中)返回一个数组时:如果该数组有一个评估为false 的条目,则整个表达式就if 而言,是false
猜你喜欢
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 2014-08-18
  • 2021-12-23
  • 2014-04-04
  • 1970-01-01
相关资源
最近更新 更多