warning: Matlab-style short-circuit operation performed for operator &

人走茶凉 提交于 2020-05-23 12:44:29

问题


Code:

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 is a column vector which contains the co-ordinates of a point. h and w are the dimensions of an image.

I am using these lines of codes in OCTAVE

But when I run the function which contains these lines I get a warning

warning: Matlab-style short-circuit operation performed for operator &

Is it that in spite of using &, octave is performing && operation?

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?

I want to check if all the statements are True and not just the first one.


回答1:


You can safely avoid the warning by using && operator instead.

The warning comes from the fact that Matlab has a special handling for & operators in this context:

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.

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.



来源:https://stackoverflow.com/questions/45461173/warning-matlab-style-short-circuit-operation-performed-for-operator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!