11.07.2015 Views

Introduction to MATLAB 7 for Engineers - The University of Jordan

Introduction to MATLAB 7 for Engineers - The University of Jordan

Introduction to MATLAB 7 for Engineers - The University of Jordan

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Lecture 6 Computer Applications 0933201 Chapter 44-12Order <strong>of</strong> precedence <strong>for</strong> opera<strong>to</strong>r types Table 4.3–2PrecedenceOpera<strong>to</strong>r typeFirst Parentheses; evaluated starting withthe innermost pair.Second Arithmetic opera<strong>to</strong>rs and logicalNOT (~); evaluated from left <strong>to</strong> right.Third Relational opera<strong>to</strong>rs; evaluated fromleft <strong>to</strong> right.Fourth Logical AND ( & ).Fifth Logical OR ( | ).Z.R.KLogical functions Table 4.3–4Logical function Definitionall(x) Returns a scalar, which is 1 if all the elements inthe vec<strong>to</strong>r x are nonzero and 0 otherwise.all(A) Returns a row vec<strong>to</strong>r having the same number <strong>of</strong>columns as the matrix A and containing ones andzeros, depending on whether or not the correspondingcolumn <strong>of</strong> A has all nonzero elements.any(x) Returns a scalar, which is 1 if any <strong>of</strong> the elementsin the vec<strong>to</strong>r x is nonzero and 0 otherwise.any(A) Returns a row vec<strong>to</strong>r having the same number <strong>of</strong>columns as A and containing ones and zeros,depending on whether or not the correspondingcolumn <strong>of</strong> the matrix A contains any nonzeroelements.finite(A) Returns an array <strong>of</strong> the same dimension as A withones where the elements <strong>of</strong> A are finite and zeroselsewhere.4-13Z.R.K13 (continued …)Logical function Definitionischar(A) Returns a 1 if A is a character array and 0 therwise.isempty(A) Returns a 1 if A is an empty matrix and 0 otherwise.isinf(A) Returns an array <strong>of</strong> the same dimension as A, withones where A has ‘inf’ and zeros elsewhere.isnan(A) Returns an array <strong>of</strong> the same dimension as A withones where A has ‘NaN’ and zeros elsewhere. (‘NaN’stands <strong>for</strong> “not a number,” which means anundefined result.)isnumeric(A) Returns a 1 if A is a numeric array and 0 otherwise.isreal(A) Returns a 1 if A has no elements with imaginaryparts and 0 otherwise.logical(A) Converts the elements <strong>of</strong> the array A in<strong>to</strong> logicalvalues.xor(A,B) Returns an array the same dimension as A and B;the new array has ones where either A or B isnonzero, but not both, and zeros where A andB are either both nonzero or both zero.4-14Logical functions Table 4.3–4 (continued)Z.R.K4-15Logical Opera<strong>to</strong>rs & and the find Function>>x = [5, -3, 0, 0, 8]; y = [2, 4, 0, 5, 7];>>z = find(x&y)z =1 2 5Note that the find function returns the indices, and notthe values.In the following session, note the difference betweenthe result obtained by y(x&y) and the result obtainedby find(x&y) in the previous example.>>values = y(x&y)values =2 4 7>>how_many = length(values)how_many =3Z.R.K<strong>The</strong> if and else Statement<strong>The</strong> if statement’s basic <strong>for</strong>m isif logical expressionstatementsendEvery if statement must have anaccompanying end statement. <strong>The</strong>end statement marks the end <strong>of</strong> thestatements that are <strong>to</strong> be executedif the logical expression is true.<strong>The</strong> basic structure <strong>for</strong> the use <strong>of</strong>the else statement isif logical expressionstatement group 1elsestatement group 2end4-16Z.R.KFlowchart <strong>of</strong> the elsestructure. Figure 4.4–2More? See pages 201-202.4-17When the test, if logical expression, isper<strong>for</strong>med, where the logical expression maybe an array, the test returns a value <strong>of</strong> true only if allthe elements <strong>of</strong> the logical expression are true!For example, if we fail <strong>to</strong> recognize how the test works,the following statements do not per<strong>for</strong>m the way wemight expect.x = [4,-9,25];if x < 0disp(’Some elements <strong>of</strong> x are negative.’)elsey = sqrt(x)endBecause the test if x < 0 is false, when this programis run it gives the resulty = 2 0 + 3.000i 5Z.R.K. 2008 Page 3 <strong>of</strong> 7Z.R.K

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!