Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Operations ENGR 1181 MATLAB 4.

Similar presentations


Presentation on theme: "Array Operations ENGR 1181 MATLAB 4."— Presentation transcript:

1 Array Operations ENGR 1181 MATLAB 4

2 Today's Learning Objectives
After today’s class, students will be able to: Explain the meaning of element-by-element operations. Identify situations where the standard operators in MATLAB (when used with arrays) are reserved for linear algebra, which is not always element-by-element. Apply dot operators for the six cases where linear algebra is not element-by-element and therefore dot operators are needed to produce element-by-element calculations.

3 Scalar Math For scalar variables a and b :
MATLAB has scalar math operations: >> a + b >> a – b >> a * b >> a / b >> a ^ b

4 Scalar–Vector Addition
Define the vector v and the scalar c : >> v = [ ] ; >> c = 4 ; Add them: >> v + c >> c + v ans =

5 Scalar - Vector Multiplication
For the vector v and scalar c : >> v = [ ] ; >> c = 4 ; Multiply them: >> c * v >> v * c ans =

6 Scalar - Vector Division
Divide: >> v / c ans =

7 Scalar - Vector Division
Divide: >> c / v >> c ./ v Error using / Matrix dimensions must agree. ans =

8 Scalar - Vector Exponents
>> v ^ c Better: >> v .^ c ans = Error using ^ Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.

9 Scalar - Vector Math Summary
For a scalar c and a vector v: Addition v + c or c + v Subtraction v – c or c – v Multiplication v * c or c * v or v.* c or c.* v Division v / c or c./ v or v./ c Exponent v.^ c or c.^ v

10 Vector–Vector Addition
Define the vector x and the vector y : >> x = [ ] ; >> y = [ ] ; Add them: >> x + y >> y + x x and y must be the same length! ans =

11 Vector - Vector Multiplication
x = [ ]; y = [ ]; Multiply: >> z = x * y ??? Error using ==> mtimes Inner matrix dimensions must agree!!!

12 Vector - Vector Multiplication
x = [ ] ; y = [ ] ; Multiply two vectors element-by-element: >> z = x .* y z =

13 Vector - Vector Division
x = [ ] ; y = [ ] ; Divide: >> x ./ y Also try >> y ./ x ans = ans =

14 Vector - Vector Exponents
y = [ ] ; Exponent: >> x .^ y Also try: >> y .^ x ans = ans =

15 Vector - Vector Math Summary
For two vectors x and y : Addition x + y or y + x Subtraction x – y or y – x Multiplication x.* y or y.* x Division x./ y or y./ x Exponent x.^ y or y.^ x Always use the dot operator for Multiply, Divide, and Exponent

16 Example 1 Calculate y = 4x2 for x = 1, 2, 3 and 4 First define x >> x = [ ]; Then calculate y >> y Which '.' is required below? y = 4.*x.^2 Required y = 4*x.^2 y = y =

17 Example 2 a = 1 2 3 4 >> y = ((4*a.^2)+a)./(2+a) y =
Calculate y = (4a2 + a)/(2+a) for a = 1, 2, 3 and 4 First define a : >> a = [ ] a = >> y = ((4*a.^2)+a)./(2+a) y =

18 Built - In Vector Functions
MATLAB has built-in functions for vectors When v is a vector: max(v) Returns the largest element in v min(v) Returns the smallest element in v mean(v) Returns the average value of the elements in v sum(v) Returns the sum of the elements of v length(v) Returns the number of elements in v sort(v) Sorts the elements of v

19 Important Takeaways Know when to use the dot operator for Multiply, Divide, and Exponent. Always use a dot operator when appropriate and understand what you are trying to accomplish when you use it. Vector functions operate on all of the numbers in a vector or matrix.

20 Preview of Next Class Input and Output
Inputting data into and out of programs GPA calculator example With and without use of vectors Inputting to a script file Output to command window

21 What’s Next? Review today’s Quiz #04
Open the in-class activity from the EEIC website and we will go through it together. Then, start working on MAT-04 homework. Before next class, you will read more detail about script files including global variables. There is also information on input and output (I/O) commands in MATLAB.


Download ppt "Array Operations ENGR 1181 MATLAB 4."

Similar presentations


Ads by Google