Relational and Logical Statements
Relational Operators in MATLAB A operator B A and B can be: Variables or constants or expressions to compute Scalars or arrays Numeric or string Operators: > (greater than) > = (greater than or equal to) < (less than) < = (less than or equal to) = = (equal to) ~ = (not equal to)
Ultimately we ll be using relational expressions in an IF statement Examples Absolute value: if a<0 a = -a end Compute letter grade: if gpa >= 90 letter_grade = A end
Logical variables The Result of comparing with a relational operator is a logical scalar or vector: If "true", MATLAB gives the comparison a value of one (1) If "false", MATLAB gives the comparison a value of zero (0)
Logical Operators Logical Operators in MATLAB are: Logical Operator & (AND) Ex. A&B Description If both operands (A and B) are true, the result is true, otherwise the result is false. (OR) A B If either operand (A and B) is true, the result is true, otherwise (both are false) the result is false. ~ (NOT) ~A Operates on 1 operand (A). Gives the opposite of the operand. True (1) if the operand is false, and false (0) if the operand is true.
Examples: Are you between 25 and 30 years old? (age>=25) & (age<=30) Is it winter? (month==12 & day>=22) (month==1) (month==2) (month==3 & day<=21)
Don t confuse == and = a == b is a relational comparison which evaluates to True or False a = b is an assignment statement, assigning the value of b to a
Round-off errors may affect == and ~=.33333 ==.33333 1 (True).33333 == 1/3 0 (False) instead, test that they are nearly equal, within some tolerance: abs(.33333-1/3) < =.0001 1
You can compare a vector with a scalar The result is a logical vector, the same size as the one being compared It helps to picture in your mind the result of a logical comparison >> x=8:12 x = 8 9 10 11 12 >> x>10 ans = 0 0 0 1 1
Example >> x=8:12 x = 8 9 10 11 12 >> x>10 ans = 0 0 0 1 1 >> x==11 ans = 0 0 0 1 0 >> x>=7 ans = 1 1 1 1 1
Compare 2 vectors They must be the same size >> A = [6 4 10]; >> B = [0 4 5]; >> A==B ans = 0 1 0
Strings are treated just like vectors >> A = Tom >> B = m % m is a scalar >> A == B ans = 0 0 1 >> Tom == Bob ans = 0 1 0
More Examples: expression 5 < 7 [ 3, 5, 2 ] > = [ 1, 0, 12 ] max( 1:6 ) < = 7 [3, pi, -12 ] > 1 'Tom' == 'Bob' 'Tom' == 'm' result 1 1 1 0 1 1 1 0 0 1 0 0 0 1
When comparing arrays They must be the same dimensions MATLAB does an element-wise comparison Result is an array that has same dimensions as other two but only contains 1's and 0's
When comparing array to scalar MATLAB compares scalar to every member of array Result is an array that has same dimensions as original but only contains 1's and 0's
Accessing Arrays Using Logical Arrays When a logical array is used to address another array, it extracts from that array the elements in the locations where the logical array has 1s. So typing A(B), where B is a logical array of the same size as A, returns the values of A at the indices where B is 1.
Example returning a scalar For example, with x = [6,3,9] and y = [14,2,9], typing z = x(x<y) finds all the elements in x that are less than the corresponding elements in y. The result is z = 6. result of x<y is [1 0 0] returns the value of x at the indices where x<y is 1
**Example returning a vector >> r = [ 8 12 9 4 23 19 10]; Define a vector r >> s = r<=10 s = 1 0 1 1 0 0 1 Checks which r elements are smaller than or equal to 10 Logical vector s with 1 s at positions where elements of r are smaller than or equal to 10 >> t=r(s) t = 8 9 4 10 Use s to select elements from vector r to create vector t Vector t consists of elements of r in positions where s has 1 s
Same result can be achieved in one step >> r = [ 8 12 9 4 23 19 10] % Extract the elements of r that are % less than or equal to 10: >> w=r(r<=10) w = 8 9 4 10
Just because an array contains only 0s and 1s, however, does not make it a logical array. >> x = [-2:2]; -2-1 0 1 2 >> i_logical = x>0; 0 0 0 1 1 >> i_double = [0 0 0 1 1]; 0 0 0 1 1 >> x(i_logical); 1 2 >> x(i_double) Subscript indices must either be real positive integers or logicals.
Array example: Score = [ 70, 55, 88, 98, 80, 73, 90] C = (Score > 70) & (Score < 81) C = [ 0 0 0 0 1 1 0 ] Useful in counting how many entries satisfy a condition: B_grades = sum( Score<91 & Score>80 )
Built-in Logical Functions MATLAB has built-in functions that are equivalent to the logical operators. and(a,b) or(a,b) equivalent to A&B equivalent to A B not(a) equivalent to ~A
Other Logical Built-in Functions Function Description Example xor(a,b) all(a) any(a) Exclusive or. Returns true (1) if one operand is true and the other is false. Returns 1 (true) if all elements in a vector A are true (nonzero). If A is a matrix, treats columns of A as vectors, returns a vector with 1 s and 0 s. Returns 1 (true) if any element in a vector A is true (nonzero). If A is a matrix, treats columns of A as vectors, returns a vector with xor(7,0) ans = 1 xor(7, -5) ans = 0 A=[6 2 15 9 7 11]; all(a) ans = 1 B=[6 2 15 9 0 11]; all(b) ans = 0 A=[6 0 15 0 0 11]; any(a) ans = 1 B=[0 0 0 0 0 0];
Other Logical Built-in Functions Function Description Example find(a) find (A>d) logical(a) If A is a vector, returns the indices of the nonzero elements. If A is a vector, returns the address of the elements that are larger than d (any relational operator can be used) Converts the elements of the array A into logical values. A=[0 9 4 3 7 0 0 1 8]; find(a) ans = 2 3 4 5 8 9 find(a>4) ans= 2 5 9
Basic Logical Operators: and & ; or ; xor xor ; not ~ Truth Table A B A&B A B xor(a,b) ~A 0 0 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 Note: xor (Exclusive Or) Returns true (1) if one operand is true and the other is false.
Operator Precedence (highest to lowest) 1. Parentheses ( ). If nested parentheses exist, inner have precedence. 2. Transpose(') and power(.^) 3. Negation (-) and logical negation (~) 4. Multiplication (.*) and division (./), 5. Addition (+) and subtraction (-) 6. Colon operator (:) 7. Relational operators (<, <=, >, >=, = =, ~=) 8. Logical AND (&) 9. Logical OR ( )
Practice logicals x = [1, 2, 3, 4, 5]; y = [-2, 0, 2, 4, 6] 1. what is the result of x < y 2. how many elements are not equal? 3. print the elements of y that are less than 0
Difference between logical expressions and Find Logical expressions return a logical scalar or array A = [1 5 7 8 0 0]; A > 5 returns [0 0 1 1 0 0] Find returns a list of indices find (A>5) returns [3, 4]
Difference between logical expressions and Find A = [1 5 7 8 0 0]; Set B equal to the non-zero elements of A 1. B = A(A~= 0) 2. B = A(find(A~=0)) Same answer. Method 2 is preferred for large sparse arrays (e.g. images)