OVERVIEW 1. Commutative and associative operators 2. Precedence of arithmetic and relational operators 3. RELATIONAL OPERATORS < > <= >= == ~= 4. Logical operators and updated PRECEDENCE TABLE 5. Other arithmetic functions 6. Review -------------------------------- 1. Commutative and associative operators a+ b = b+a for all a,b + is commutative (and so is *, min,max) But 10-0 is not equal to 0-10 so - is NOT commutative (/ is not as well) (a+b)+c = a+(b+c) for all a,b + is associative (and so is *,min,max, but not - and /). * / ^ \ + - are binary operators : they need two operands left and right to operate on left op right where op is one of * / ^ \ + - Operator Operation * multiplication / division \ back-division ^ exponentiation + addition - subtractionThe operation for But operators + - are ALSO unary operators Operator Operation + plus sign - negative sign 2.Precedence of arithmetic and relational operators PRECEDENCE ( ) parentheses ^ unary + , unary - / * \ + - < > <= >= == ~= Higher precedence operators are evaluated first; If same precedence operators (more than one) appear in an expression evaluation is left to right Thus 1-2-3 becomes (1-2)-3 1*2/3/4 becomes ((1*2)/3)/4 USE PARENTHESES IN ALL CASES where there is more than one operator in an expression ! 2/4/2 is ugly without parentheses. It evaluates to (2/4)/2= 0.25 A 2^2^2^2 is you looking for trouble: ((2^2)^2)^2 is 256 5-+5 and 5+-5 should be avoided! Playing with five Unary + - Binary + - What is the value of --5 --5 --5 ---5---5 ARITHMETIC OPERATORS +5 ans = 5 -5 ans = -5 ----------5 ans = 5 clear clc 5 / 5 / 5 ans = 0.2000 5 / 5 * 5 / 5 ans = 1 5 + 5 / 5 -5 * 5 ans = -19 2-2-2 ans = -2 2^2^2^2 ans = 256 2^2^1^1 ans = 4 ---5 * ----5 ans = -25 3. RELATIONAL OPERATORS < > <= >= == ~= They all have the same precedence when we compare two arguments (operands) the result can be true or false. In MATLAB lower-case true has value 1 and false value 0. >> true ans= 1 >> whos ans 1x1 1byte logical array The data type of true, false is logical and uses 1 byte. However if you perform arithmetic operations, result ans gets typecast to a double >> true*true ans=1 >> whos ans 1x1 8 bytes double array >> FALSE = 1; >> FALSE == true ans =1 false is lower-case false. Upper case FALSE is nothing unless the user assigns a value to it. CAUTION: MATLAB prints the value 1 for true. It never prints true itself. 1<2<3<4 is (1<2)<3<4 is 1<3<4 is (1<3) < 4 is 1<4 is 1 Playing with FIvE 5 == 5 == 5 is 0 since (5==5)==5 is (1 == 5) is 0. How about 5 <= 5 <= 5 or 1<2>1 Note 0==0 /1 and 0/0 == 1 5 ~= false is 1 (true) since it is 5 ~= 0 which is true. aN and Inf NaN == NaN is 0 NaN ~= Nan is 1 but 5/ 0 == 1/0 is 1 PRECEDENCE and RELATIONAL OPERATORS 1 + 2 == 2 ans = 0 whos Name Size Bytes Class Attributes ans 1x1 1 logical 1 + ( 2 == 2 ) ans = 2 whos Name Size Bytes Class Attributes ans 1x1 8 double 5 == 5 == 5 ans = 0 5 < 5 < 5 ans = 1 whos whos Name Size Bytes Class Attributes ans 1x1 1 logical 5 == 5 + 5 ^ 5 - 5 ans = 0 NaN == NaN ans = 0 Inf == Inf ans = 1 Inf == -Inf ans = 0 false-4 ans = -4 ans = -4 whos Name Size Bytes Class Attributes ans 1x1 8 double 1 < 2 > 3 < 4 ans = 1 0/0 == 1 ans = 0 0/0 ans = NaN 4. Logical Operators Logical operators a | b or(a,b) disjunction or inclusive-or a & b and(a,b) conjunction ~ b not(b) negation xor(a,b) exclusive-or a || b shortcut-or a && b shortcut-and The semantics of || and && are those of | and & respectively not(b) : evaluate b, if b is true, not(b) false if b is false, not(b) true a&b : evaluate a,b if a true AND b true, a&b is true else a&b is false (think of a*b) [i.e. both true to be true] a|b : evaluate a,b if a false AND b faslse, a|b is false else a|b is true (think of a+b) [one or more true to be true] xor(a,b): evaluate a,b if one true and the other false xor(a,b) is true else xor(a,b) is false [exactly one true + exactly one false to be true] Shortcut && || a && b : evaluate a if a is false a&& b is false (do not evaluate b) else evaluate b to determine a&&b based on b a || b : evaluate a if a is true a|| b is true (do not evaluate b) else evaluate b to determine a||b based on b Thus in shortcut || && one may derive the answer by evaluating only half of the arguments! 0 && toast and 1 || toast give 0 and 1 but 0 & toast or 1| toast generate errors (toast not defined). ~1 - ~0 : ~ has priority over - 1 & 21>10 : & has lower priority (under relational operators). false | true & false true | false & true 0 && toast and 1 || toast give 0 and 1 but 0 & toast or 1| toast generate errors (toast not defined). 0 && (1/0) (no warning because division will not evaluated/ performed) 0 & (1/0) ( warning because division will be evaluated/ performed) AN UPDATED PRECEDENCE TABLE parentheses () ^ ' (transpose) .^ .' unary + unary - , ~ (logical not) * / \ .* ./ .\ + - : < > <= >= == ~= & | xor && || 1 & 0 ans = 0 1 | 1 ans = 1 xor(1,1) ans = 0 ~ 1 ans = 0 ~ true ans = 0 5 & 6 ans = 1 Name Size Bytes Class Attributes ans 1x1 1 logical xor(10,10) ans = 0 xor(10,0) ans = 1 ~5 ans = 0 -~0 ans = -1 whos Name Size Bytes Class Attributes ans 1x1 8 double ~-0 ans = 1 whos Name Size Bytes Class Attributes ans 1x1 1 logical 1- ~0 ans = 0 ~1 - ~0 ans = -1 whos Name Size Bytes Class Attributes ans 1x1 8 double 1 & 1 > 10 whos Name Size Bytes Class Attributes ans 1x1 1 logical ~-5 - ~5 ans = 0 0 && toast ans = 0 1 || toast ans = 1 | toast Error using toast Too many output arguments. >> ~ true ans =0 >> true+ true ans = 2 >> whos ans double array >> true | true ans = 1 >> whos ans logical array Playing with five ~ 5 ~ 5 -5 ~5 -true ~-5 and -~5 5 & 5 > 5 5 & 5 >= 5 5. Other arithmetic functions sqrt(4) ans = 2 abs(-2) ans = 2 abs(2) ans = 2 mod(10,4) ans = 2 mod(-10,4) ans = 2 >> a = [ 10 20 4 50 30 40] % length(a) : length of vector : # of elements >> length(a) ans = 6 >> min(a) % find min of elements of a vector a ans = 4 >> max(a) % find max ans = 50 >> sort(a) % sort value in nondecreasing order <= <= <= ans = 4 10 20 30 40 50 >> sum(a) % sum ans = 154 >> prod(a) % product ans = 48000000 >> sum(a)/length(a) % average value of elements of a ans = 25.6667 But min and max can return two values. To collect two values form a row vector of two values >> [x y] = min(a) x=4 y=3 % x is the minimum value and y is the index of a with min value is a(y) is x ! >> floor(1.3) ans = 1 >> ceil(1.3) ans = 2 >> floor(-1.3) ans = -2 >> ceil (-1.3) ans = -1 >> round(1.3) ans=1 >> round(1.5) ans=2 % floor(x) : largest integer <= x % ceil (x) : smallest inte >= x % round(x) : floor(x+0.5) % sin,cos, tan etc but be careful with sin(pi) it's small but not ZERO! >> abs(-10) ans=10 >> sqrt(9) ans=3 >> sqrt(-1) ans = 0+ 1.0000i % i.e. i ! 6. REVIEW 2^2 ans = 4 2^2^2^2 ans = 256 5/5/5 ans = 0.2000 1 == 1 ans = 1 whos Name Size Bytes Class Attributes ans 1x1 1 logical 5 == 5 ans = 1 5 == 5 == 5 ans = 0 5 <= 5 <= 5 ans = ans = 1 5 ~= 5 ~= 5 ans = 1 5 > 5 < 5 ans = 1 5 + 5 == 5 ans = 0 (5 + 5) == 5 ans = 0 5 + ( 5 == 5 ) ans = 6 whos Name Size Bytes Class Attributes ans 1x1 8 double 2 / 4 / 2 ans = 0.2500 2 * 4 * 3/ 4 ans = 6 5 -- 5 ans = 10 5 -------------------------- 5 ans = 10 true ~= false ans = 1 whos Name Size Bytes Class Attributes ans 1x1 1 logical true + false ans = 1 whos Name Size Bytes Class Attributes ans 1x1 8 double 1 < 2 < 3 < 2 ans = 1 Inf == Inf ans = 1 Inf == - Inf ans = 0 NaN == NaN ans = 0 5 ~= 5 ~= 5 ans = 1 1==2 ans = 0 whos Name Size Bytes Class Attributes ans 1x1 1 logical 5 == 5 == 5 ans = 0 0 & 1 ans = 0 whos Name Size Bytes Class Attributes ans 1x1 1 logical 5 & 5 ans = 1 xor(5,0) ans = 1 ~ 5 ans = 0 ~-5 ans = 0 ~5 - true ans = -1 whos Name Size Bytes Class Attributes ans 1x1 8 double ~-5 - ~5 ans = 0 whos Name Size Bytes Class Attributes ans 1x1 8 double 5 & 5 > 5 / 5 ans = 1 whos Name Size Bytes Class Attributes ans 1x1 1 logical 0 & toast Too many output arguments. 1 | toast Too many output arguments. 0 && toast ans = 0 1 || toast ans = 1 0 & (1 / 0) ans = 0 NaN == NaN ~true - ~false ans = -1 whos Name Size Bytes Class Attributes ans 1x1 8 double exp(1.0) ans = 2.7183 abs(-30) ans = 30 abs(30) ans = 30 abs(0) ans = 0 mod(10,4) ans = 2 mod(-10,4) ans = 2 sqrt(4) ans = 2 clear clc a=int8(5); b=5; c='5'; whos Name Size Bytes Class Attributes a 1x1 1 int8 b 1x1 8 double c 1x1 2 char