Relational and Logical Operators MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Both operators take on form expression1 OPERATOR expression2 and evaluate to either TRUE (1) or FALSE (0) Relational operators: Lisa A. Oberbroeckling Loyola University Maryland loberbroeckling@loyola.edu >> 1 < 2 1 < > <= <= == = >> 1 > 2 0 The relational operators produce matrices of 0s and 1s if one of the arguments is a matrix. L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 1 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 2 / 1 Logical Operators s &, && AND (eager version,short-circuit version), OR (eager version, short-circuit version) NOT xor Exclusive OR all TRUE if all elements of vector are nonzero any TRUE if any elements of vector are nonzero Eager version will evaluate both expressions no matter what. The Short-circuit versions are only good on scalars and will only evaluate the second expression if needed. Pros and cons to each version in complicated programs - search for Short-circuit evaluation on Wikipedia for examples. The eager version of AND, OR, and NOT logical operators produce matrices of 0s and 1s if one of the arguments is a matrix. >> x = [-1 1 1]; y = [1 2-3]; >> x>0 & y >0 0 1 0 >> x>0 y>0 1 1 1 >> xor(x>0,y>0) 1 0 1 >> any(x>0) 1 >> all(x>0) 0 >> if x>0 && sin(1/x)<0.5 % one use of short-circuit L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 3 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 4 / 1
Order of Precedence Flow Control More functions in book! Precedence Operation 1 Parentheses 2 Exponents 3 Logical NOT 4 Multiplication/division 5 Addition/subtraction 6 Relational operators 7 Logical AND (&) 8 Logical OR ( ) 1 if- 2 if-- 3 if-if... - 4 switch 5 for 6 while The first four are conditional statements The last two are loops L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 5 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 6 / 1 IF statement IF ELSE and IF ELSEIF... ELSE if b ~=0 && (a == 5 a==3) y=a/b; z=a+b; if x >= 0 absx = x; absx = -x;. L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 7 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 8 / 1
sgn.m function s = sgn(x) % SGN(X) computes the sign or signum of the number X % if x < 0 s = -1; if x > 0 s = 1; sgn2.m function s = sgn2(x) % SGN2(X) Computes the sign or signum of the number X. % First checks that X is a number. if length(x) ~= 1 error( Input of sgn2 function is not a number ) if x < 0 s = -1; if x > 0 s = 1; L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 9 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 10 / 1 Pseudocode and Testing SWITCH statement Pseudocode (from dictionary.com) a program code unrelated to the hardware of a particular computer and requiring conversion to the code used by the computer before the program can be used. Pseudocode is especially useful to plan out your program(s) on scratch paper before typing - what variables do you need? What conditional statements need to be done, and how? How to set up the loops, etc. Testing: try and think of easy accuracy checks, but also error checks. switch expression case value case value. otherwise switch mod(n,2) case 0 disp( n is even! ) case 1 disp( n is odd! ) otherwise disp( n is not an integer! ) This can be easier in some instances than an if-if- statement L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 11 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 12 / 1
FOR loop for indexvariable = a:i:b MATLAB command(s) indexvariable does not need to be declared before; it can be new (and probably should be new) a:i:b or a:b has the indexvariable start at a and at (or going no further than) b, with an increment of i. (remember, if the increment is left out, the increment is 1). indexvariable = vector also works (see forex2.m) There is no need to increment the indexvariable; that is automatically done geomseries.m function s = geomseries(r, n) % GEOMSERIES(N) computes the sum of the geometric sequence % of R from k=0 to N % for k=0:n s = s + r^k; GOOD FOR SERIES! L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 13 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 14 / 1 WHILE loop While PSEUDOCODE while something is true do something Statements are executed as long as expression is true. Nice commands: break and continue read about it (also can be used in FOR loops) Something must change so loop isn t infinite! Read Chapter 7 and the H-drive for more details, syntax and examples! whileex1a.m function y = whileex1a(n) % WHILEEX1A(N) adds the numbers from 1 to N % L Oberbroeckling y=0; k=1; while k <= n y = y+k; k = k+1; L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 15 / 1 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 16 / 1
Notes on Nesting Loops and conditional statements can be nested within themselves Keep organized by having nice spacing (USE TABS) nest1 nest2 nest3 L. Oberbroeckling (Loyola University) MATLAB 10/07/10 Lecture 17 / 1