ENG College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical Operations with Arrays : Chapter 3
ENG Types of Matrix Arithmetic There are two types of matrix arithmetic Matrix math is used in Linear Algebra to solve simultaneous equations (MATLAB text Ch. 3.2 and beyond the scope of this course) We will only introduce element-by-element arithmetic, e.g. If A = [a b c] and B = [x y z], then A + B = [a+x b+y c+z]
ENG Types of array arithmetic There are only six cases where matrix math and element-by-element arithmetic differ A*B A/B A^B A^s s^A s/A For these cases, the standard operator has been chosen to represent matrix arithmetic, so we need a new symbol for element-by element arithmetic (dot operators) A.*B A./B A.^B A.^s s.^A s./A If A and B are matrices and s is a scalar,
ENG The vectors must be the same size. Element-by-element operations for row vectors: If:a = [a1 a2 a3] and b = [b1 b2 b3] Then: a.* b = [a 1 * b 1 a 2 * b 2 a 3 * b 3 ] a./ b = [a 1 / b 1 a 2 / b 2 a 3 / b 3 ] a.^ b = [a 1 ^b 1 a 2 ^b 2 a 3 ^b 3 ] a.^2 = [a 1 ^2 a 2 ^2 a 3 ^2] 2./a = [2/a 1 2/a 2 2/a 3 ] Examples of Dot Operators
ENG Element-by-element operations for matrices: Then: Given:and Examples of Dot Operators
ENG MATRIX ELEMENT-BY-ELEMENT EXAMPLES >> A = [1, 2, 3; 4, 5, 6] A = >> B = [1, 2, -1; 2, -2, 5] B = >> A.* B ans = >> A./ B ans = >> B.^ 3 ans =
ENG Dot Operators If you forget a dot, when one is required, you will either get a cryptic error message (involving matrix dimensions) or you will get the wrong answer For the following operations: s+A, s-A, A-s, A+B, A-B, s*A, A/s matrix math is the same as element-by- element arithmetic, so dot operators are optional.
ENG SCALAR-ARRAY ADDITION AND SUBTRACTION >> M = M = >> a = s + M a = >> b = s - M b = >> c = M - s c = The scalar operates on each element in the array. For example, given the following matrix M and the scalar s = 2 we would get the following results for addition and subtraction
ENG SCALAR-ARRAY MULTIPLICATION AND DIVISION the scalar operates on each element in the array For example, given the same matrix M and scalar s = 2 we would get the following results for multiplication and division >> M = M = >> d = s * M d = >> d = M / s d =
ENG ARRAY-ARRAY ADDITION AND SUBTRACTION The arrays being used in the operation must have the same size. The sum (difference) of two arrays is obtained by adding (subtracting) corresponding array elements, resulting in a new array of the same size. >> A = [1 2; 3 4] A = >> B = [1 -1; 2 0] B = >> C = A + B C = >> A - B ans =
ENG CALCULATING THE VALUE OF A FUNCTION For the function: calculate y for z = 1, 3, 5, 7, 9, 11, SOLUTION USING MATLAB: >> z = [1: 2: 15] z = Create a vector z with seven elements. Where do you have operations between vectors that need a dot operator? >> y = (z.^ * z)./ (4 * z.^ ) y =
ENG Element-by-Element Calculations NOTE: Even though the calculation is written only one time in the previous example, MATLAB iterates through all elements of z (Element-by-Element) and calculates a corresponding number of elements of solution array y.
ENG SOME USEFUL BUILT-IN ARRAY FUNCTIONS MATLAB has many built-in functions that can be used with arrays. Some of them are (A is a vector): max(A)Returns the largest element in A min(A)Returns the smallest element in A mean(A)Returns the average value of the elements in A sum(A)Returns the sum of the elements of A length(A)Returns the number of elements in A sort(A)Sorts the elements of A The Help window gives information about many other functions
ENG EXAMPLES OF BUILT IN FUNCTIONS >> sum(A) ans = 48 >> length(A) ans = 6 >> sort(A) ans = >> A = [ ] A = >> max(A) ans = 14 >> min(A) ans = 2 >> mean(A) ans =
ENG APPLICATION Element-by-element calculations are useful in processing data and in calculating the value of a mathematical function at many points. EXAMPLE: The coefficient of friction μ is determined by measuring the force F required to move a mass μ: μ = F / (mg) g = 9.81 m/s 2 The results from measuring F in five tests are given in the table. Determine the coefficient of friction in each test, and the average from all tests. m Ffriction 69 Mass: m (kg) Force: F (N)
ENG >> mass = [ ]; >> force = [ ]; >> mu = force./ (9.81 * mass) mu = >> mu_ave = mean(mu) meu_ave = COEFFICIENT OF FRICTION USING MATLAB 69 Create the mass vector. Create the force vector Calculate mu for each mass-force pair, using element-by-element calculations. Determine the average of the elements in the vector mu by using the function mean().