240 likes | 775 Views
MatLab – Chapman S3.3-4, Part 1 Relational Operators, Logical Operators and Conditional Statements. 3.3.1 Relational Operators (p. 88). MATLAB has 6 relational operators to make comparisons between variables. See Table 3-1 (p. 88). < less than > greater than
E N D
MatLab – Chapman S3.3-4, Part 1Relational Operators, Logical Operators and Conditional Statements
3.3.1 Relational Operators (p. 88) • MATLAB has 6 relational operators to make comparisons between variables. See Table 3-1 (p. 88). • < less than • > greater than • <= less than or equal to • >= greater than or equal to • == equal to • ~= not equal to
RELATIONAL OPERATORS • Results of comparison using relational operators: • ZERO, if comparison is false. • False = 0 • ONE, if comparison if true. • True = 1 • If comparing numbers, any non-zero is considered “true”
EXAMPLE >> x=2; >> y=3; >> z=x<y; % same as z = (x<y) >> u=x==y; % same as u = (x==y) >> z,u z = 1 u = 0 Here z and u are “logical” variables (p. 157 for array use).
3.3.3 Logical Operators • MATLAB has five logical operators ( also called Boolean operators) shown in Table 3.2 (p. 90): • ~ (NOT): z = ~x. • & (AND): used to link logical expressions: z =(x<y) & (a>b). • | (OR): used to link logical expressions: q =(x<y) | (a>b). • && (Short-Circuit AND): used for operations on 2 scalar logical expressions. • || (Short-Circuit OR): used for operations on 2 scalar logical expressions. • (Note | is the shift-\ key, above Enter). • xor (exclusive OR) is used to link logical expressions: w = xor(A, B).
ORDER OF PRECEDENCE • First: parenthesis, innermost first. • Second: arithmetic operators and logical NOT (~) • Evaluated from left to right. • Third: relational operators • Evaluated from left to right. • Fourth: logical AND. • Fifth: logical OR.
THE NOT OPERATOR ( ~ ) • Given an array A: • ~A returns an array of the same dimension. • ~A has ones where A is zero and zeros where A is nonzero.
EXAMPLE of the ~ Operator >> x = [6, 3, 9]; y = [14, 2, 9]; >>z = ~x z = [0, 0, 0] >>z = ~x > y % same as z = (~x) > y z = [0, 0, 0] >>z = ~( x > y) z = [1, 0, 1]
EXAMPLE continued • What would z = (x>~y) return? • How about z = (x~>y)? • How about z = (x~=y)? • After you write down what you think they return, type them into Matlab (Recall x = [6, 3, 9]; y = [14, 2, 9])
Practice • For the arrays x and y in the previous example, show using MATLAB that z = ~( x > y) is equivalent to z = ( x <= y ) (Recall x = [6, 3, 9]; y = [14, 2, 9])
THE AND OPERATOR ( & ) • The & operator compares two arrays A and B of the same dimension and returns an array of the same dimension. • Returns ONE when both of the corresponding elements of A and B are non-zero. • Returns ZERO when either or both of the corresponding elements of A and B are zero.
EXAMPLE of the & Operator • z = 0 & 3 returns z = 0. • z = 2 & 3 returns z = 1. • z = 0 & 0 returns z = 0. • z = [ 5, -3, 0, 0] & [ 2, 4, 0, 5] returns z = [1, 1, 0, 0].
ORDER OF PRECEDENCE FOR OPERATOR TYPES (AGAIN) • First: parenthesis, innermost first. • Second: arithmetic operators and logicalNOT (~) • Evaluated from left to right. • Third: relational operators • Evaluated from left to right. • Fourth: logical AND. • Fifth: logical OR.
THE OR OPERATOR ( | ) • The | operator compares two arrays A and B of the same dimension and returns an array of the same dimension. • Returns ONE when either or both of the corresponding elements of A and B are non-zero. • Returns ZERO when both of the corresponding elements of A and B are zero.
EXAMPLE of the | OPERATOR • z = 0|3 returns z = 1. • z = 0|0 returns z = 0. • z = [ 5, -3, 0, 0] | [2, 4, 0, 5] returns z = [ 1, 1, 0, 1]. • What value does the expression below assign to z?z = 3 < 5 | 4 ==7 • Test in Matlab.
XOR (Exclusive OR) • xor(A,B) return zeros where A and B are either both nonzero or both zero. It returns ones where either A or B is nonzero, but not both. • This can be written as a function: function z = xor(A,B) z = (A|B) & ~(A&B);
Example of xor and | Operators • z = xor([3,0,6], [5,0,0]); • Returns z = [0,0,1]. • z = ([3,0,6] | [5,0,0]); • Returns z = [1,0,1].
RELATIONAL AND LOGICAL FUNCTIONS • MATLAB provides additional relational and logical functions: • any(x) • all(x) • find(x) p282 • finite(x) • etc.
Comparisons • Note: Truth TablesA good way to deal with complex logic problems. Some people do not use it much and prefer to combine switch, if and while structures.