Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Math.

Similar presentations


Presentation on theme: "Array Math."— Presentation transcript:

1 Array Math

2 I. Definition MATLAB is known for its mathematical power!
It does vector and MATRIX operations automatically (most languages require you to do this by hand). It follows the rules of ANY math book Rules of addition and subtraction are straightforward and highly common sense: think MATH Rules of multiplication and division require basic knowledge of matrix math This is an “n-by-p” matrix. n rows, p columns. Wikipedia

3 II. addition/subtraction (example1)
Assume the vectors (actual math vectors) shown here >>v1+v2+v3 <enter> results in? v1 = 2 -3 v2 = 4 -2 v3 = -1 4 ans = ans = 5 -1 (a) (b)

4 II. addition/subtraction (example2)
Assume the matrices A and B below. >>A+B <enter> results in? A = B = ans = ans = (a) (b)

5 II. addition/subtraction (example3)
Assume the matrices A and B below. >>A+B <enter> results in? A = B = ans = ans = (a) (b) (c)

6 II. addition/subtraction (example4)
Assume the matrices A and B below. >>C = B-A <enter> results in? A = B = ans = ??? Error using ==> minus Matrix dimensions must agree. Neither (a) nor (b) (c) (a) (b)

7 II. addition/subtraction (Rule #1)
Addition and subtraction between arrays (vectors OR matrices) can be done if and only if BOTH dimensions match. (2 x 3) (2 x 3) By “dimensions”, understand “number of rows of A and B are equal, and number of columns of A and B are equal” This does not mean “Square” A 3by4 can be added or subtracted to a 3by4. A 100by200 can be added or subtracted to a 100by200. A 5by4 cannot be added to a 4by5. A 5by5 can be added to a 5by5.

8 Exception to rule #1 Adding or subtracting a scalar (a 1 by 1 array) to any array is possible. MATLAB assumes the operation is done on each element by default. A = >> A+2 <enter> results in? ans = 2 is a 1by1 Answer: A is a 3by5 It still works!

9 Multiplication/Division/Power
Linear Algebra has its own way of multiplying matrices. You’ll learn all about it in MA 345. MATLAB defaults to matrix multiplication.

10 Matrix Multiplication the Linear Algebra way
Matrices can be multiplied together if and only if the inner dimensions match (2 x 3) * (3 x 3) = (2 x 3) (4 x 3) * (3 x 1) = (4 x 1) The resulting dimension is the outer dimensions. “inner dimensions”

11 Matrix Multiplication the Linear Algebra way
A is a (2by3) matrix and B is a (3by4) matrix: True or False >>A*B <enter> is a valid statement True or False >>B*A <enter> is a valid statement >>C = A*B C will be a __ by __ matrix. >> B*A Error using * Inner matrix dimensions must agree. 2 4

12 Matrix Division To divide a matrix A by a matrix B really means:
“To multiply matrix A by the inverse of matrix B” “inverse” does not mean “transpose”, nor “reciprocal”  All the rules of multiplications apply The inverse of a matrix is possible if and only if it is “square”: it has the same amount of rows as columns. >> A/B for example: (2by3) / (3by3) means (2by3) * inv(3by3) means (2by3) * (3by3) will work. a/b is really a*(1/b)

13 But I’m not in MA 345 yet… The majority of how you’ll use matrices in EGR115 will NOT be using the linear algebra method.

14 Multiplying/Dividing by a Scalar
Multiplying together a matrix and a scalar (a 1 by 1 array) to any array is possible. MATLAB assumes the operation is done on each element per default. Note: the inner dimension obviously don’t match, so A*A wouldn’t work. A = >> A*2 <enter> results in? Answer:

15 Multiplying/Dividing by a Scalar
Dividing a matrix by a scalar (a 1 by 1 array) is possible. MATLAB assumes the operation is done on each element bydefault. CAUTION: Dividing a scalar by an array is impossible.

16 What if arrays don’t represent MATRICES
In MATLAB, an array of multiple dimensions represents a MATRIX. Any operation (+-*/^) on these is dealt with following strict mathematical rules What if an array is simply used as a database of numbers? use the element by element operators .* ./ .^ A = A^3 = A^3 IS NOT 5^ ^3 8^ ^3 * * =

17 Assume the two following vectors:
Example Calculating Prices Assume the two following vectors: One is the quantity bought of each item (1-5) The second is the price of each item (1-5) Calculate the total bill ($) So: quantities.*prices , then add all? 2 3 6 4 quantities 20.00 10.50 5.50 10.00 25.00 prices 2* 20 3* 10.5 6* 5.5 4* 10 25 + + + + = $219.50

18 The Dot Operator Any time you need to do math on an element-by-element basis (which will be most of the time in EGR115), you need to use the dot before the multiplication, division, and exponentiation sign. A = A^3 = A^3 IS NOT 5^ ^3 8^ ^3 * * = A.^3 IS 5^ ^3 8^ ^3

19 Array Functions

20 Basic functions on arrays
CAUTION: A function will return different values depending on whether the argument is a vector or a matrix! Built-in function Argument is a vector Argument is a matrix min() Returns the minimum value of the vector Extracts the minimum of EACH column -> returns a vector max() Returns the maximum value of the vector Extracts the maximum of EACH column mean() NOT avg()!!!! AUTOMATICALLY adds all the elements in the vector together and divides by the number of elements! Extracts the average of EACH column

21 Basic functions on arrays
Rule of thumb: If your input is a vector, it will apply the function to all the elements in the array, regardless of if the vector is a row or column vector If your input is a matrix, it will apply the function to each column and return a vector corresponding to the results from each column.

22 Ex. vector: Temperatures
clc clear % engine temperature temps = [45.5,56.7,99.9,42,12,29]; % determine minimum,maximum,average temperature minTemp = min(temps) maxTemp = max(temps) avgTemp = mean(temps) Do not use min =, or max=…, or mean= … These keywords are reserved by MATLAB!

23 Ex. matrix: Scores clc clear % scores from playing. Col1 = player1, col2 = player2…etc. scores = [45,34,56; 67,3,45; 22,55,99]’; %transposed! % determine minimum,maximum,average scores minScores = min(scores) ? maxScores = ______________ avgScores = ______________


Download ppt "Array Math."

Similar presentations


Ads by Google