1 / 22

Array Operations

Array Operations. ENGR 1181 MATLAB 04. Array Operations In The Real World.

star
Download Presentation

Array Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Operations ENGR 1181 MATLAB 04

  2. Array Operations In The Real World Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane. These vortices can cause unstable flight conditions for smaller planes approaching the wake of a commercial jetliner. Thus this turbulent flow data is important in airspace management and stall design.

  3. Today's Learning Objectives • Scalar Math • Vector Math • Scalar – Vector Math • Vector – Vector Math • Vector Functions

  4. Scalar math review For scalar variables a and b: >> a = 6; >> b = 2; MATLAB has scalar math operations: >> a + b >> a – b >> a * b >> a / b >> a ^ b

  5. Scalar–vector addition Define the vector v and the scalar c: >> v = [ 10 20 30 40 ] ; >> c = 4 ; Add them: >> v + c >> c + v ans = 14 24 34 44

  6. Vector–vector addition Define the vector v and the vector c: >> v = [ 10 20 30 40 ] ; >> c = [ 2 4 6 8] ; Add them: >> v + c >> c + v v and c must be the same length! ans = 12 24 36 48

  7. Scalar-vector multiplication For the vector x and the scalar c: >> v = [ 10 20 30 40 ] ; >> c = 4 ; Multiply them:>> c * v>> v * c ans = 40 80 120 160

  8. Vector-vector multiplication x = [ 10 20 30 40 ] y = [ 2 4 6 8 ] Now multiply: >> z = x * y ??? Error using ==> mtimes Inner matrix dimensions must agree!!!

  9. Vector-vector multiplication x = [ 10 20 30 40 ] y = [ 2 4 6 8 ] To multiply to arrays element by element we need to use the following syntax: >> z = x .* y z = 20 80 180 320

  10. Scalar-vector division >> v = [ 10 20 30 40 ] >> c = 4 Divide them: >> v / c ans = 2.50 5.00 7.50 10.00

  11. Scalar-vector division >> v = [ 10 20 30 40 ] >> c = 4 Divide them: >> c / v >> c./v Error using / Matrix dimensions must agree. ans = 0.400 0.200 0.133 0.100

  12. Vector-vector division x = [ 10 20 30 40 ] y = [ 2 4 6 8 ] Divide them: >> x . / y Also, try >> y . / x ans = 5 5 5 5 ans = 0.20 0.20 0.20 0.20

  13. Scalar-vector exponents >> v = [ 10 20 30 40 ] >> c = 4 Try instead: >> v .^ c Try: >> v ^ c Error using ^ Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.

  14. Vector-vector exponents x = [ 2 2 2 2 ] ; y = [ 2 4 6 8 ] ; Also try >> y .^ x Try this: >> x .^ y ans = 4 16 64 256 ans = 4 16 36 64

  15. 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

  16. 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 You must alwaysuse the dot operator for Multiplication, Division, andExponent

  17. Example 1 Calculate y = 4x2for x = 1,2,3 and 4. First define x >> x = [1 2 3 4]; Then calculate y >> y = 4.*x.^2Which ‘.’ is required here? y = 4 16 36 64

  18. Example 2 Calculate y = (4a2 + a)/(2+a) for a = 1,2,3 and 4. First define a = [ 1 2 3 4 ] >> a = [1 2 3 4]; a = 1 2 3 4 >> y = ((4*a.^2)+a)./(2+a) y = 1.6667 4.5000 7.8000 11.3333

  19. Built-in vector functions MATLAB has built-in functions for vectorsWhen 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

  20. Important Takeaways • Know when to use a DOT Operator for Multiplication, Division, and Exponents. • Only use a dot operator when appropriate and understand what you are trying to accomplish before you use it. • Vector functions operate on an entire set of numbers located inside of an array, or matrix.

  21. Preview of Next Class • Inputs and outputs with script files • 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

  22. Tasks to Complete • 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.

More Related