error while using Operands

1 次查看(过去 30 天)
Tarek Eid
Tarek Eid 2020-3-29
Hi all
im trying to get this code to run but whenever i do i keep getting this error
if BSgrid(K)~=0 || BSgrid(K+1)~=0 || BSgrid(K+2)~=0 && direction==1
K=BSgrid(K)==0 && K+1==BSgrid(K+1)==0 && K+2==BSgrid(K+2)==0
elseif BSgrid(K)~=0 || BSgrid(K+10)~=0 || BSgrid(K+20)~=0 && direction==2
K=BSgrid(K)==0 && K+10==BSgrid(K+10)==0 && K+20==BSgrid(K+20)==0
end
operands to the || and && operators must be convertible to logical scalar values.
Error in battleshipfinal (line 143)
if BSgrid(K)~=0 || BSgrid(K+1)~=0 || BSgrid(K+2)~=0 && direction==1
what does it mean? and how do i get rid of it

回答(1 个)

the cyclist
the cyclist 2020-3-29
编辑:the cyclist 2020-3-29
Here's what is means:
true && true; % This works, and gives a scalar result
[true true] && [true false]; % This gives the error you see
The && and || operators are "short-circuit logical operators". They can be used only on scalar values -- not vectors -- because they have to be able to evaluate to simply a single true or false value, not a vector of values.
If you want a vector of logical values, you should use
[true true] & [true false]; % This works, and gives a vector result

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by