Relational and Logical Operators

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

Info iconThis preview shows pages 1–4. 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
Background image of page 1

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

View Full DocumentRight 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
Background image of page 2
Array dimensions must match for binary array op. Example 8.1.4
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2011 for the course EGM 4320 taught by Professor Klee during the Spring '11 term at University of Central Florida.

Page1 / 14

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online