how to convert and solve error Operands to the || and && operators must be convertible to logical scalar values.

8 views (last 30 days)
Samir Prajapati
Samir Prajapati on 29 Jul 2014
Commented: Patrik Ek on 29 Jul 2014
??? 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

Answers (1)

Patrik Ek
Patrik Ek on 29 Jul 2014
Edited: Patrik Ek on 29 Jul 2014
You can only use && for scalar inputs. For vector use either & for elementwise or isequal to check if vectors are equal.
  2 Comments
Patrik Ek
Patrik Ek on 29 Jul 2014
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by