Hello, why Matlab does not check for second condition and give an answer without checking second condition after AND short circuit operator?

4 vues (au cours des 30 derniers jours)
Hello everyone!
Kindly ask help in if statement. If I want to write..
if plane00 ==plane1 OR plane00==plane3 AND plane01==plane1 OR plane01==plane3, then print ('Planes are parallel1')
when I test the code below it only check for one condition isequal(plane00, plane1) || isequal(plane00, plane3)) and then supposed is true if it is true, but did not check second condition after && operator
(isequal(plane01,plane1))||isequal(plane01,plane3)
Thnak you in advance for your time and consideration. Apreciate any help
if (isequal(plane00, plane1) || isequal(plane00, plane3)) && ((isequal(plane01,plane1))||isequal(plane01,plane3))
fprintf('Planes are parallel1:');
ThetaBar = -(Theta0);
PhiBar = -(Phi0);
fprintf('ThetaBar: %f\n',ThetaBar);
fprintf('PhiBar: %f\n',PhiBar);
% I also tried to do smth like this
%(plane01 == 1 || 3 && plane00 == 1 || 3)
  1 commentaire
Walter Roberson
Walter Roberson le 11 Avr 2023
Despite your "supposed is true", (isequal(plane00, plane1) || isequal(plane00, plane3)) is probably false.
For debugging, break it into two pieces:
part1 = isequal(plane00, plane1) || isequal(plane00, plane3);
disp(part1)
if part1 && ((isequal(plane01,plane1))||isequal(plane01,plane3))

Connectez-vous pour commenter.

Réponse acceptée

Askic V
Askic V le 10 Avr 2023
Hi Aknur,
in the example below, the part after && is evaluated.
BTW, operators || and && are so called short-circuit operators.
https://www.mathworks.com/help/matlab/ref/shortcircuitand.html
plane00 = 1;
plane1 = 1;
plane01 = 3;
plane3 = 3;
if (isequal(plane00, plane1) || isequal(plane00, plane3)) &&...
(isequal(plane01,plane1) || isequal(plane01, plane3))
fprintf('True');
end
True
  2 commentaires
Aknur
Aknur le 11 Avr 2023
Hi, Dear @Askic V thank you for your answer and clarification.
Yes it works perfect. I change my code and it works as a separate script. But it does not work especially for my code inside of function. Plane00 and plane01 are variables. In some case it give me True answer but in some False( but shoud be true)
Appreciate any advice what can be wrong with my statement or shoudl I try to do it using normal
plane00 = 4
plane01 = 3
parallel_planes =
1 3
2 4
5 6
plane1 = parallel_planes(1,1)
plane2 = parallel_planes(1,2)
plane3 = parallel_planes(2,1)
plane4 = parallel_planes(2,2)
plane5 = parallel_planes(3,1)
plane6 = parallel_planes(3,2)
if (isequal(plane01,plane1)||isequal(plane01,plane3)) && (isequal(plane00, plane1) || isequal(plane00, plane3))
fprintf('Planes are parallel5:');
ThetaBar = -(Theta0);
PhiBar = -(Phi0);
fprintf('ThetaBar: %f\n',ThetaBar);
fprintf('PhiBar: %f\n',PhiBar);
elseif (isequal(plane00,plane2)||isequal(plane00,plane4)) && (isequal(plane01, plane2) || isequal(plane01, plane4))
fprintf('Planes are parallel2:');
ThetaBar = -(Theta0);
PhiBar = -(Phi0);
fprintf('ThetaBar: %f\n',ThetaBar);
fprintf('PhiBar: %f\n',PhiBar);
elseif (isequal(plane00,plane5)||isequal(plane00,plane6)) && (isequal(plane01, plane5) || isequal(plane01, plane6))
fprintf('Planes are parallel3:');
ThetaBar = -(Theta0);
PhiBar = -(Phi0);
fprintf('ThetaBar: %f\n',ThetaBar);
fprintf('PhiBar: %f\n',PhiBar);
else
ThetaBar = -(Theta0);
PhiBar = (180 - Phi0);
fprintf('Planes are not parallel:');
fprintf('ThetaBar: %f\n',ThetaBar);
fprintf('PhiBar: %f\n',PhiBar);
end
Thank you in advance
Stephen23
Stephen23 le 11 Avr 2023
This is your code run here on the forum. So far it seems that && and || are working as expected.
plane00 = 4;
plane01 = 3;
parallel_planes = [1,3;2,4;5,6];
plane1 = parallel_planes(1,1)
plane1 = 1
plane2 = parallel_planes(1,2)
plane2 = 3
plane3 = parallel_planes(2,1)
plane3 = 2
plane4 = parallel_planes(2,2)
plane4 = 4
plane5 = parallel_planes(3,1)
plane5 = 5
plane6 = parallel_planes(3,2)
plane6 = 6
if (isequal(plane01,plane1)||isequal(plane01,plane3)) && (isequal(plane00, plane1) || isequal(plane00, plane3))
fprintf('Planes are parallel5:');
% ThetaBar = -(Theta0);
% PhiBar = -(Phi0);
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
elseif (isequal(plane00,plane2)||isequal(plane00,plane4)) && (isequal(plane01, plane2) || isequal(plane01, plane4))
fprintf('Planes are parallel2:');
% ThetaBar = -(Theta0);
% PhiBar = -(Phi0);
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
elseif (isequal(plane00,plane5)||isequal(plane00,plane6)) && (isequal(plane01, plane5) || isequal(plane01, plane6))
fprintf('Planes are parallel3:');
% ThetaBar = -(Theta0);
% PhiBar = -(Phi0);
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
else
% ThetaBar = -(Theta0);
% PhiBar = (180 - Phi0);
fprintf('Planes are not parallel:');
% fprintf('ThetaBar: %f\n',ThetaBar);
% fprintf('PhiBar: %f\n',PhiBar);
end
Planes are parallel2:

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by