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

2 Ansichten (letzte 30 Tage)
??? 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

Antworten (1)

Patrik Ek
Patrik Ek am 29 Jul. 2014
Bearbeitet: Patrik Ek am 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 Kommentare
Michael Haderlein
Michael Haderlein am 29 Jul. 2014
Bearbeitet: Michael Haderlein am 29 Jul. 2014
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
Patrik Ek am 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

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by