Presentation is loading. Please wait.

Presentation is loading. Please wait.

To add, subtract, multiply, and divide, absolutely follow the mathematical rules. 1. All the rules 2. The element-per-element operator 3. Ex1: graphing.

Similar presentations


Presentation on theme: "To add, subtract, multiply, and divide, absolutely follow the mathematical rules. 1. All the rules 2. The element-per-element operator 3. Ex1: graphing."— Presentation transcript:

1 To add, subtract, multiply, and divide, absolutely follow the mathematical rules. 1. All the rules 2. The element-per-element operator 3. Ex1: graphing 4. Ex2: calculating a bill Arithmetic Ops’ on Arrays 11

2 Every Operation is Different! 2 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ ???? ???? ???? ???? ???? ???? “Safe” or “Caution”?

3 Scalar  Scalar +-*/ All operations (+ - * / ) are valid, no exceptions. >> A=2; >> B=-3; A goes first B goes first 3

4 Every Operation is Different! 4 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ ???? ???? ???? ???? ????

5 Scalar  Vector + All safe! >> n = 2; >> vr = [1 2 3]; %row vector >> vc = [-1; -2; -3]; %colum vector Results are identical. 5

6 Scalar  Vector - All safe! >> n = 2; >> vr = [1 2 3]; %row vector >> vc = [-1; -2; -3]; %colum vector Results are opposite when flipped. 6

7 Scalar  Vector * All safe! >> n = 2; >> vr = [1 2 3]; %row vector >> vc = [-1; -2; -3]; %colum vector Identical results when flipped 7

8 Scalar  Vector / CAUTION! >> n = 2; >> vr = [1 2 3]; %row vector >> vc = [-1; -2; -3]; %colum vector All good when vector goes first 8

9 Scalar  Vector / CAUTION! >> n = 2; >> vr = [1 2 3]; %row vector >> vc = [-1; -2; -3]; %colum vector Strange result when scalar goes first! 9 2 [1 2 3] 2 -2 -3 “Multiplies by the inverse vector”… Dividing by a row goes against the rules in math.

10 Scalar  Vector./ Now, when the intention is to execute the division on EACH element of the array, a new operator exists: The element-per-element operator:./ “dot-divide” 10 2/-1 2/-2 2/-3 2 2 2 1 2 3

11 Every Operation is Different! 11 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ ???? ???? ???? ????

12 Scalar  Matrix +-* Assume a scalar and a 2 by 4 matrix >> n = 2; >> mat = [1 2 3 4; 5 6 7 8]; Addition, subtraction, and multiplication are fine, regardless of who goes first. Below are addition and multiplication examples only: 12 Can you guess who has issues…

13 Scalar  Matrix /./ Dividing by a scalar is fine! Each element is divided by 2. 13 Dividing of course…

14 Scalar  Matrix /./ Dividing by a scalar is fine! Each element is divided by 2. Once again, to divide a scalar by a matrix is invalid. Use the “dot-divide” to divide the scalar by EACH element. 14 Again, this is due to rules in mathematics. 2 2 1 2 3 4 2 2 5 6 7 8 Dividing of course…

15 Every Operation is Different! 15 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ ???? ???? ????

16 Every Operation is Different! 16 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ ???? ???? ???? “Safe” or “Caution”?

17 Vectors, Matrices +- The rule is the same, whether applied to vectors or matrices. “Given variables that are not scalars, they can be added or subtracted (in any directions) if and only if their dimensions are identical!” Matlab simply adds (or subtracts) the elements placed in the same location (row, col). Therefore, each element must have its ‘brother/sister’ in the other variable! 17 + ???

18 Vectors, Matrices +- Vectors + - 18 Matrices + - Note: Subtracting the opposite way gives the opposite result. Both matrices are 2 by 3’s. Both vectors are 1 by 3’s.

19 Ex1: Adding Vectors 19 Source: hyperphysics.phy-astr.gsu.edu/hbase/vect.htmlhyperphysics.phy-astr.gsu.edu/hbase/vect.html 9 3 10 17 ? ? Advantage of arrays? 3 variables instead of 6 (one for each value)!!!

20 Ex1: Adding Vectors There is no limit to the amount/directions of vectors. The dimensions of each vector must match: here [x, y] 20 Source: hyperphysics.phy-astr.gsu.edu/hbase/vect.htmlhyperphysics.phy-astr.gsu.edu/hbase/vect.html

21 Every Operation is Different! 21 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ ?? ?? ?? As long as the dimensions are exactly the same.

22 Every Operation is Different! 22 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ ?? ?? ?? As long as the dimensions are exactly the same.

23 Vectors, Matrices * / It is possible to multiple and divide arrays together, as long as the “MATRIX-EXPRESSION” is valid. not, Not, NOT as long as the dimensions match! Remember: MATLAB = “Matrix Laboratory” Multiplying vectors/matrices with the * operator alone does NOT multiply each element by its brother/sister in the same location. Same for divide, it does NOT mean element-by- element. 23 So.. What does it do…

24 Multiplication only * For example It is mathematically doing what it is supposed to do: “Matrix Multiplication”. What must be understood: the * operator alone is NOT meant to multiply element-by-element. 24 63 9-5 42 132213222 3 by 22 by 2 6*1+3*2 6*3+3*2 9*1-5*2 9*3-5*2 4*1+2*2 4*3+2*2 3 by 2 *

25 Multiplication only, cont. To multiply matrices and/or vectors using the * operator alone, the “inner-dimensions must match”. Visually, this means: 25 *................ R1 by C1 R2 by C2 C1 must be equal to R2

26 Multiplication only, cont. To multiply matrices and/or vectors using the * operator alone, the “inner-dimensions must match”. Visually, this means: 26 *................ R1 by C1 R2 by C2 C1 must be equal to R2........ R1 by C2 Ironically, the resulting matrix has R1 rows and C2 columns (the exterior dimensions..)!

27 Multiplication only, cont. 2 more examples: The “Inner-Dimensions must match” 27 6 9 4 1313 4 by 1 1 by 2 6*1 6*3 9*1 9*3 4*1 4*3 -1*1-1*3 * 6 9 4 13471347 4 by 1 2 by 2 * 6 18 9 27 4 12 -1 -3 4 by 2

28 Multiply element-per-element If the intention is NOT TO MATRIX-MULTIPLY, but to regard the array as a collection of data and apply EQUATIONS TO EACH ELEMENT, use the element- per-element operator.* “dot-multiply”. This is best shown in examples. 28

29 Ex1. Plotting graphs Recall an example from the chapter “Creating Arrays” x = linspace(-10,10,20); %array of 20 data points y = x.*x; %calculate array of y’s %(The dot will be explained next time…) plot(x,y) %plot command Statements that would create errors: y = x*x; y = x^2; Matlab would try to do vector-multiplication! Statement that would work as well: y = x.^ 2; %the “dot-caret” operator 29

30 Ex2. Calculating 30

31 Division only / Using the / operator alone between vectors/matrices tells Matlab to “multiply by the inverse”. If it is the intention of the programmer, all is good! Note: if you have not learned what “multiply by the inverse” mean, no panic as we will not ask you to explicitly apply this. However, you must be able to just state what / does. 31

32 Division only, cont. If the intention, however, is to divide each element of a vector by the elements of another (same for matrices), use the element-per-element operator./ “dot-divide”! The (r by c) dimensions must exactly match for both variables Example for vectors: 32

33 Division only, cont. Example for matrices: A and B are both 2by3’s (2rows by 3 columns). Note how A/B gives back a 2by2, not a 2by3. 33

34 Use the operators wisely! 1. Are you doing math? 2. Are you doing element-per- element operations? Wrapping Up Rules are: The element-per-element operators are:.* and./ 34 Scalar Vector Matrix Vector Scalar Vector Matrix Scalar Vector Matrix +-*/ As long as the dimensions are exactly the same.


Download ppt "To add, subtract, multiply, and divide, absolutely follow the mathematical rules. 1. All the rules 2. The element-per-element operator 3. Ex1: graphing."

Similar presentations


Ads by Google