logical operators in if statement
174 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
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
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.
Respuestas (3)
Andrei Bobrov
el 14 de Ag. de 2014
2 comentarios
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.
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
0 comentarios
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
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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!