Relational and Logical Operators

Relational and Logical Operators - Chapter 8 Relational and...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 8 Relational and Logical Operations 8.1 Relational Operators Relational and logical operators are instrumental in program flow control. They are used in MATLAB m-Files to test various conditions involving variables and expressions. The relational operators are listed below. Relational Operator Description < less than <= less than or equal to > greater than >= greater than or equal to == equal to ~= not equal to Relational operators can be used to compare elements of a numerical array to a scalar. The result is an identically sized array of 1's and 0's indicating the results of the comparison. The resulting array is called a logical array. Example 8.1.1 mu=10; sigma=1; n=7; x=mu+sigma*randn(1,n) % Generate array x of n Normally distributed % numbers with mean mu and std deviation sigma high1=x>mu % Find components of x which exceed mu and store in logical % array high1 x = 10.7119 11.2902 10.6686 11.1908 8.7975 9.9802 9.8433 high1 = 1 1 1 1 0 0 0 x low1=x<mu % Find components of x which are less than mu and store in % logical array low1 x = 10.7119 11.2902 10.6686 11.1908 8.7975 9.9802 9.8433 low1 = 0 0 0 0 1 1 1 x high2=x>mu+sigma % Find components of x which are greater than mu + % sigma and store in logical array high2 x = 10.7119 11.2902 10.6686 11.1908 8.7975 9.9802 9.8433 1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
high2 = 0 1 0 1 0 0 0 x low2=x<mu-sigma % Find components of x which are less than mu - % sigma and store in logical array low2 x = 10.7119 11.2902 10.6686 11.1908 8.7975 9.9802 9.8433 low2 = 0 0 0 0 1 0 0 Example 8.1.2 first_name_1='Harold'; last_name_1='Klee'; first_name_2='Robert'; last_name_2='Pratt'; first_name_3='Harold'; lastd_name_3='Smith'; first_name_4='Michael'; last_name_4='Grant'; size_fn_1=size(first_name_1) % Get size of character string % first_name_1 size_fn_2=size(first_name_2) % Get size of character string % first_name_2 size_fn_1==size_fn_2 % Check if 1°2 arrays size_fn_1 and % size_fn_2 are equal match_fn_12=first_name_1==first_name_2 % Check if characters in % first_name_1 and characters in first_name_2 are % equal and store results in logical array match_fn_12 disp(' ') last_name_2==last_name_4 size_fn_1 = 1 6 size_fn_2 = 1 6 ans = 1 1 match_fn_12 = 0 0 0 0 0 0 ans = 0 1 1 0 1 Be careful when checking if two character strings are equal. Remember character strings are actually character string arrays and consequently must be the same size before a comparison using the logical operator '==' can be performed. The following example illustrates the result of trying to compare different size character strings. Example 8.1.3 size(last_name_1) % Find size of character string last_name_1 size(last_name_2) % Find size of character string last_name_2 last_name_1==last_name_2 % Invalid operation because last_name_1 and % last_name_2 are character string arrays of different sizes ans = 1 4 ans = 1 5 ??? Error using ==> == 2
Image of page 2
Array dimensions must match for binary array op. Example 8.1.4 A=[1 5 3; 9 2 5; 6 0 2] B=eye(3) % Create 3°3 Identity matrix AB=A==B % Check locations where A and B are equal and store 1's in % those locations of logical array AB and 0's elsewhere C=2+3*ones(3) AC=A~=C % Check locations where A and C are not equal and store 1's in % those locations of logical array AC and 0's elsewhere A = 1 5 3 9 2 5 6 0 2 B = 1 0 0 0 1 0 0 0 1 AB = 1 0 0 0 0 0 0 1 0 C = 5 5 5 5 5 5 5 5 5 AC = 1 0 1 1 1 0 1 1 1 3
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8.2 Logical Operators
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern