logical operators in if statement

174 visualizaciones (últimos 30 días)
richard
richard el 14 de Ag. de 2014
Comentada: Guillaume el 7 de Jul. de 2016
I have a code that looks like
for x = 1:7:length(list)
if(list(x+2)>0 && list(x+2)<1024)
energys(list(x+2))=energys(list(x+2))+1;
end
end
"list" is a column vector and "energys" is a row vector of zero's from 1,1024
When the logical expression say evaluates at 2000, (greater than 0, but also greater than 1024), does my loop end? Can someone explain the && operator in the context of my code?
  3 comentarios
Stephen23
Stephen23 el 7 de Jul. de 2016
Editada: Stephen23 el 7 de Jul. de 2016
@Kenneth Lamury: what you proposed does not make any difference, because the operator precedence rules clearly state that > and < have a higher precedence than &&. So adding brackets around the two equivalence operations makes no difference whatsoever, because the operator precedence already ensures that those comparisons are performed before the && operation.
In fact the original question has too many brackets already:
if list(x+2)>0 && list(x+2)<1024
does exactly the same thing. Why make it more cluttered than it needs to be ?
Guillaume
Guillaume el 7 de Jul. de 2016
It seems a lot of people like to enclose their if expression in brackets (a hold-over from C?), including Mathworks in some of their own m-files (see actxserver.m for example)
I agree with Stephen. This makes it look cluttered for no apparent benefit.

Iniciar sesión para comentar.

Respuestas (3)

Andrei Bobrov
Andrei Bobrov el 14 de Ag. de 2014
about && see here
  2 comentarios
richard
richard el 14 de Ag. de 2014
I know, I read this before I posted but it doest give a scenario where expr1 is true but expr2 is false.
Steven Lord
Steven Lord el 7 de Jul. de 2016
true and false is false regardless of whether you write "and" as & or &&.
In the case you described you'd be executing essentially "if false, < stuff >, end" and that will not execute the body of the if statement.
But in this case, you don't really need a loop. Extract the appropriate elements of list as a vector, use logical indexing on that vector as a whole to select only those elements in the range (0, 1024), then accumulate using accumarray.

Iniciar sesión para comentar.


Iain
Iain el 14 de Ag. de 2014
Your loop's final execution (barring errors) will have x = length(list)
list(x+2) is liable to throw an error when x + 2 is greater than length(list)
In this code:
if x && y
disp('then')
else
disp('else')
end
&& simply applies the "and" operation to x and y

Adam
Adam el 14 de Ag. de 2014
Editada: Adam el 14 de Ag. de 2014
The logical expression you have won't cause your loop to end other than if it crashes as described by Iain. It will simply cause the expression in the if statement to either happen or not.
for x = 1:7:length(list)
is the only thing that controls your loop termination. If you had a break statement in an else part of your if then that would terminate the loop, but otherwise the logical expression is independent of loop termination
  2 comentarios
richard
richard el 14 de Ag. de 2014
OK I understand what you mean now, and I understand how my loop could crash as described by lain, but the funny thing is that this doesn't crash when seemingly it should for the reasons described by lain.
This subfunction I have is part of a larger code...Would you be interested in looking at a part of it?
Adam
Adam el 14 de Ag. de 2014
Editada: Adam el 14 de Ag. de 2014
Well it isn't guaranteed to crash because you increment in jumps of 7 so
1:7:length(list)
will produce an array whose largest number is at most the length of the list, but must also be increments of 7 away from 1. So if e.g. your list length was 10 it would just evaluate to
x = 1:7:10 == [1 8]
which would not crash your code because your x + 2 will have a maximum value of 10 which is a valid index into your list.
If your code is guaranteed to always fall into this situation where incrementing by 7 will always leave you at least 2 below your list length then it will not crash. If your list length is arbitrary though then it will crash any time incrementing by 7 takes you within 1 of your list length.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements 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