1 / 19

MatLab – Chapman S3.3-4, Part 1 Relational Operators, Logical Operators and Conditional Statements

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

Download Presentation

MatLab – Chapman S3.3-4, Part 1 Relational Operators, Logical Operators and Conditional Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MatLab – Chapman S3.3-4, Part 1Relational Operators, Logical Operators and Conditional Statements

  2. 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

  3. 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”

  4. 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).

  5. 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).

  6. 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.

  7. 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.

  8. 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]

  9. 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])

  10. 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])

  11. 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.

  12. 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].

  13. 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.

  14. 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.

  15. 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.

  16. 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);

  17. 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].

  18. RELATIONAL AND LOGICAL FUNCTIONS • MATLAB provides additional relational and logical functions: • any(x) • all(x) • find(x) p282 • finite(x) • etc.

  19. 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.

More Related