Filter löschen
Filter löschen

logical operators in if statement

164 Ansichten (letzte 30 Tage)
richard
richard am 14 Aug. 2014
Kommentiert: Guillaume am 7 Jul. 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 Kommentare
Stephen23
Stephen23 am 7 Jul. 2016
Bearbeitet: Stephen23 am 7 Jul. 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 am 7 Jul. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Andrei Bobrov
Andrei Bobrov am 14 Aug. 2014
about && see here
  2 Kommentare
richard
richard am 14 Aug. 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 am 7 Jul. 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.

Melden Sie sich an, um zu kommentieren.


Iain
Iain am 14 Aug. 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 am 14 Aug. 2014
Bearbeitet: Adam am 14 Aug. 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 Kommentare
richard
richard am 14 Aug. 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 am 14 Aug. 2014
Bearbeitet: Adam am 14 Aug. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by