警告:为操作员和驾驶员执行的Matlab式短路操作 [英] warning: Matlab-style short-circuit operation performed for operator &
问题描述
代码:
if (round(xw(1))>2) & (round(xw(2))>2) & (round(xw(1))<h-1) & (round(xw(2))<w-1)
W0 = img(round(xw(1))-2:round(xw(1))+2,round(xw(2))-2:round(xw(2))+2);
else
NA=1;
break
endif
xw
是包含点坐标的列向量.
h
和w
是图像的尺寸.
xw
is a column vector which contains the co-ordinates of a point.
h
and w
are the dimensions of an image.
我正在 OCTAVE
但是当我运行包含这些行的函数时,我会得到警告
But when I run the function which contains these lines I get a warning
warning: Matlab-style short-circuit operation performed for operator &
是不是使用&
,但 octave 仍在执行&&
操作?
Is it that in spite of using &
, octave is performing &&
operation?
我了解到,如果我使用&&
,则根据第一条语句是True
还是False
,将评估下一条语句.
I learnt that if I use &&
then depending on the first statement is True
or False
, the next statements are evaluated.
那么,当我收到此警告时,这是怎么回事?那么该问题的解决方案是什么?
So, is this what is happening when I get this warning? What is the solution to this problem then?
我想检查所有语句是否都是True
而不仅仅是第一个.
I want to check if all the statements are True
and not just the first one.
推荐答案
通过使用&&
运算符可以安全地避免该警告.
You can safely avoid the warning by using &&
operator instead.
警告来自 Matlab具有特殊处理方式对于&
运算符,在这种情况下:
The warning comes from the fact that Matlab has a special handling for &
operators in this context:
当您使用元素级&和|上下文中的运算符 if或while循环表达式(仅在该上下文中),它们使用 短路以评估表达式.
When you use the element-wise & and | operators in the context of an if or while loop expression (and only in that context), they use short-circuiting to evaluate expressions.
出于兼容性的原因,Octave会检测到此行为并模拟Matlab的行为.请注意,在Matlab中也完全可以使用&&
,因为无论如何这都是隐式使用的.
For reasons of compatibility, Octave detects this behaviour and emulates what Matlab does. Note that its completely safe to use &&
also in Matlab since that is what is implicitly used anyways.
这篇关于警告:为操作员和驾驶员执行的Matlab式短路操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!