how to convert and solve error Operands to the || and && operators must be convertible to logical scalar values.
조회 수: 1 (최근 30일)
이전 댓글 표시
??? Operands to the and && operators must be convertible to logical scalar values.
Error in ==> idenity at 15 if((AUU==A)&&(AINTU==A)&&(AINTX==A)&&(AUX==X));
please solve this error and give reply
below is my program to prove property of identity rule
U=input('Input set U :'); A=input('Input set A :'); X=input('Input set X'); AUU=union(A,U); disp(AUU); AINTU=intersect(A,U); disp(AINTU);
AINTX=intersect(A,X); disp(AINTX); AUX=union(A,X); disp(AUX); %indx=((AUU==A)&(AINTU==A)&(AINTX==A)&(AUX==X)); %if((AUU(indx)==A(indx))&&(AINTU(indx)==A(indx))&&(AINTX(indx)==A(indx))&&(AUX(indx)==X(indx))) if((AUU==A)&&(AINTU==A)&&(AINTX==A)&&(AUX==X)); fprintf('Identity Rule is Proved'); else fprintf('Identity Rule is Failed'); end
댓글 수: 0
답변 (1개)
Patrik Ek
2014년 7월 29일
편집: Patrik Ek
2014년 7월 29일
You can only use && for scalar inputs. For vector use either & for elementwise or isequal to check if vectors are equal.
댓글 수: 2
Michael Haderlein
2014년 7월 29일
편집: Michael Haderlein
2014년 7월 29일
Here, that would only shift the problem: if will only work with scalar true/false, not with vectors. At least one of your variables AUU, A, AINTU, AINTX and AUX is non-scalar. Maybe you need the functions all/any to make a useful statement. edit: Patrik's suggestion isequal might also be the key. Only elementwise won't help.
Patrik Ek
2014년 7월 29일
My assumption was that the code assumed that the the vectors would have the same length. In other case elementwise operations does of course not make sense. In that case it is still possible to do a complete comparison with isequal. Else you can do a length comparison and work elementwise if the length is the same and use isequal otherwise (or always use isequal if this is what you need the example is not very clear on that point).
However & goes perfectly well with eqally sized matrices
a = true(2,2); b = [true, false; false, true];
c = a&b
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!