A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB
Complement on Matrix manipulation 1 Relational operations Example >> A=rand(4,3) >> B=(A>.2)&(A<0.8) >> C=A.*B >> D=(A~=A(1,1)) 2 Logical operations
I NTRODUCTION TO M ATLAB 3 Operator Precedence You can build expressions that use any combination of arithmetic, relational, and logical operators. Precedence levels determine the order in which MATLAB evaluates an expression. Within each precedence level, operators have equal precedence and are evaluated from left to right. The precedence rules for MATLAB operators are shown in this list, ordered from highest precedence level to lowest precedence level: 1. Parentheses () 2. Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^) 3. Unary plus (+), unary minus (-), logical negation (~) 4. Multiplication (.*), right division (./), left division(.\), matrix multiplication (*), matrix right division (/), matrix left division (\) 5. Addition (+), subtraction (-) 6. Colon operator (:) 7. Less than ( ), greater than or equal to (>=), equal to (==), not equal to (~=) 8. Element-wise AND (&) 9. Element-wise OR (|)
I NTRODUCTION TO M ATLAB >> a|b&c is similar to >> a|(b&c) >> A = [3 9 5]; >> B = [2 1 5]; >> C = A./B.^2 C = >> C = (A./B).^2 C = Built-in functions and(A,B) equivalent to A & B or(A,B) equivalent to A | B not(A) equivalent to ~A Returns 1 for a vector where all elements of the vector are true (nonzero), and 0 if all elements are not true all Returns 1 for a vector where any element of the vector is true (nonzero), and 0 if no elements are true. any Find indices and values of nonzero elementsfind Sort array elements in ascending or descending ordersort Note: The all and any functions ignore any NaN values in the input arrays
I NTRODUCTION TO M ATLAB >> X = [ ]; >> indices = find(X) >> find(X > 2) >> find(X == 0) >> X = [ ]; >> indices = find(X) >> find(X > 2) >> find(X == 0) Returns the mean values of the elements along different dimensions of an array mean Returns the largest (smallest) element along different dimensions of an array max, min Return the sum of the elements along different dimensions of an array sum Calculates differences between adjacent elements of a vector diff Returns the products of the elements prod Returns the cumulative sum along different dimensions of an array. cumsum Returns the cumulative product along different dimensions of an array. cumprod 4 Some functions
I NTRODUCTION TO M ATLAB Examples >> a=rand(1,5) >> max(a) >> A=rand(3,6) >> max(A) >> max(max(A)) >> sort(a) >> sort(A) >> mean(a) >> mean(A) >> max(A) >> max(max(A)) >> a=rand(1,5) >> max(a) >> A=rand(3,6) >> max(A) >> max(max(A)) >> sort(a) >> sort(A) >> mean(a) >> mean(A) >> max(A) >> max(max(A))
I NTRODUCTION TO M ATLAB b returns the scalar product of the vectors a anddot(a,b) returns the cross product of the vectors a and b.cross(a,b) >> a = [1 2 3]; b = [4 5 6]; >> c = cross(a,b) >> d = dot(a,b) 5. Format function The format Function : controls the numeric format of the values displayed by MATLAB. The function affects only how numbers are displayed, not how MATLAB computes or saves them. >> x = [4/ e-6] format short format short e e e-006 format short g e-006 format long format long e e e-00 format long g e-006 format bank format rat 4/3 1/ Special Characters % : Insert comment line into code >> x=[1:5] %create an row vector from 1 to 5 increment 1