error while using Operands
11 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!