about easy logic operators

2 visualizaciones (últimos 30 días)
Umut Oskay
Umut Oskay el 29 de Abr. de 2020
Comentada: David Hill el 29 de Abr. de 2020
A=[2 4 10 9 24;3 5 56 -1 7];
for i = 1: 5
if A(1,i) && A(2,i) < 0 % if 9 && -1 < 0
fprintf('Invalid dimension.The area cannot be computed.\n')
else
fprintf('The area of rectangle %d is %d.\n',i,(A(1,i)*A(2,i)));
end
end
if A(1,i) && A(2,i) < 0 % if 9 && -1 < 0 i think the left one and the right one is the same but the outputs they give are different . Why are they different? Thanks.

Respuestas (1)

David Hill
David Hill el 29 de Abr. de 2020
Need <0 for both conditions
A=[2 4 10 9 24;3 5 56 -1 7];
for i = 1: 5
if A(1,i)<0 && A(2,i)< 0 % if 9 && -1 < 0
fprintf('Invalid dimension.The area cannot be computed.\n')
else
fprintf('The area of rectangle %d is %d.\n',i,(A(1,i)*A(2,i)));
end
end
  2 comentarios
Umut Oskay
Umut Oskay el 29 de Abr. de 2020
Editada: Umut Oskay el 29 de Abr. de 2020
A=[2 4 10 9 24;3 5 56 -1 7];
for i = 1: 5
if (A(1,i)< 0) || (A(2,i)< 0)
fprintf('Invalid dimension.The area cannot be computed.\n')
else
fprintf('The area of rectangle %d is %d.\n',i,(A(1,i)*A(2,i)));
end
end
% i got the point and i should write like this thank you but i want to know why [A(1,i) && A(2,i) < 0 % 9 && -1 < 0] are different?
David Hill
David Hill el 29 de Abr. de 2020
Matlab syntax for logicals. Any number other than zero is a logical true value.
9 && -1<0; %true && true = true
A(1,1) && A(2,1)<0;%true && false = false

Iniciar sesión para comentar.

Categorías

Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by