1 / 29

Arrays – Part2 of many

Arrays – Part2 of many. Hard-coding vectors and matrices (review) Referencing Common Operations: 1. Slicing 2. Diminution 3. Augmenting Common Functions: min(), max(), mean(), prod(), sum(). I. Hardcoding Review – The Symbols. [] are used to hardcode values into an array

jeroen
Download Presentation

Arrays – Part2 of many

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. Arrays – Part2 of many Hard-coding vectors and matrices (review) Referencing Common Operations: 1. Slicing 2. Diminution 3. Augmenting Common Functions: min(), max(), mean(), prod(), sum()

  2. I. Hardcoding Review – The Symbols • [] are used to hardcode values into an array • _________ are optional, but they separate COLUMNS • _______________are necessary if you want to hardcode a new row • ___________ is used to transpose a vector/matrix • “Flip it”: 1st column becomes 1st row, 2nd column becomes 2nd row… • ________are useful when the values have an “addition-pattern” • BAD vectors or matrices

  3. I. Hardcoding Review – Regular arrays • At this time, do not mix letters and numbers within a single array. An array will either contain all numbers (float and integers combined), OR it will contain all single characters. • “Cell-arrays” will be capable of combining both.

  4. Example Work Done on an object Physics In physics, work is the amount of energy transferred to a body by a force acting through a distance. The work (in Joules) done by this person pulling the weight is: W = F . d F d dot product 9 12.3 3 2 7 5.2 F = d =

  5. Example System of Linear Equations Any topic… 3x-2y = 9 (1) -8x +4y =12 (2) x = ? y = ? >>>Substitution Method<<< Take equation (2) and solve analytically for y : 4y = 12 + 8x -> y = 3 + 2x Substitute into (1), solve numerically for x : 3x -2*(3+2x) = 9 -> x = -15 Substitute x into (2), solve for numerical y: y = -27

  6. Example System of Linear Equations Any topic… 3x-2y = 9 (1) -8x +4y =12 (2) x = -15 y = -27 In vector notation: A x = b x y 3 -2 -8 4 9 12 = * “Matrix A” “Vector x” “Column Vector b” -> Solve using matrix math: (A-1 *A) x = A-1 *b

  7. II. Array Referencing • Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions of the array or even individually. • Because the values contained within the array may change when the program runs, the index(i.e. position) of the elements allows a mean of accessing them. • MATLAB starts counting at 1. 3RD

  8. II. Array Referencing • How to refer to elements within a scalar? A vector? A matrix? • A scalar has one single value – simply refer to the variable itself. age • A vector has one dimension regardless whether it’s a row vector or a column vector. Use a single index to reference the values in a vector: ages(2) • A matrix has two or more dimensions. Use an index for EACH dimension: FIRST: a row number, SECOND: a column number pressures(3,56) (More dimensions? Use another number for each additional dimension!)

  9. Example 1 GPA calculations Referencing arrays Calculate your semester G P A How many classes? How many A’s? --> worth 4 How many B’s? --> worth 3 How many C’s? --> worth 2 How many D’s? --> worth 1 How many F’s? --> worth 0

  10. Example 1 GPA calculations Referencing arrays Solve by hand How many classes? 6 How many A’s? 2 --> worth 4 --> 8 How many B’s? 2 --> worth 3 --> + 6 How many C’s? 1 --> worth 2 --> + 2 How many D’s? 1 --> worth 1 --> + 1 How many F’s? 0 --> worth 0 --> + 0 17 --> 17/6 = 2.8333

  11. Example 1 GPA calculations Referencing arrays Variables? How many classes? 6 <-- a scalar Quantity for each letter grade <-- a vector Execution

  12. II. Array Referencing • More than likely, programs use variables as indices (not constants, as shown before): • Instead of saying M(2,3), write: ships = … ; %a 2D array with values 0’s or 1’s r=input('What row would you like to guess? '); c=input('And what column in this row: '); if ships(r, c) == 1 %boat is hit %do stuff else%no boat %do other stuff end

  13. Example2 Solving system of equations Referencing matrices 3x-2y = 9 (1) -8x +4y =12 (2) x y 3 -2 -8 4 9 12 = * Prompt the user for the number of equations (n), and each value. Loop to create matrix A and vector b. Solve for x & y.

  14. Example2 Solving system of equations Referencing matrices In math books:  1st row 3 -2 -8 4  2nd row A11 A12 A21 A22 2nd column 1st column In programming: A(1,1) A(1,2) A(2,1) A(2,2) Ask for 1 row at a time, then loop.

  15. III. Common Operations • Three common operations on arrays are: • SLICING: referencing to a piece of the array • Either to copy it to another location • Or to delete it from its location • Or to replace it by other values • Or to do math operations on the values • CAUTION: not necessarily “delete” • DIMINUTION: deleting elements of an array • Rule: keep the array rectangular at all cost! • AUGMENTING: increasing the size of the array by storing more values within the array.

  16. III.1. Array Slicing • In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice. % Copy all elements in rows 1 and 2, % that are in columns 1 through 4

  17. III.1. Array Slicing • In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice. % Copy all elements in rows 1 and 2 % that are in columns 1 through 4 M1 = M(___,____); 1:2 1:4

  18. Example 1 Keeping valid data In wind tunnels, the data is obtained throughout the tunnel. However, data is usually flawed around the walls, or far away form the object itself. Given an array of pressure/temperature/or density obtained, only the ones far from the wall are kept for analysis!

  19. III.1. Array Slicing (Shortcuts) • Hardcoding is great when the number of elements, or when the size of the array is known. But what if it isn’t know? • Keep all the values except the first and last 2? • Keep all the values except the borders (i.e. 1st and last row, 1st and last column)? • The keyword end is very friendly when it comes to slicing. • IT WILL NOT BE BLUE! vector2 = vector1( 2 : end-2 ); matrix2 = matrix1( 2:end-1 , 2:end-1);

  20. III.2. Array Diminution • To delete elements from an array, assign the empty vector: __________ = [ ]; • THIS ACTION CANNOT BE UNDONE. You may have to restart the program to get back to the original values. • You can delete: • A single element from a vector • A single row or column from a matrix • Multiple rows/columns from a matrix • All values within any array of any size

  21. Example “Diminution” • Create a vector x, and a matrix y, just so we can test and practice “diminution” • %delete the 5th element of x • %delete the 1st element of x • %delete an entire column of y • %delete an entire row of y • %delete element (2,5) of y • %delete the 1st and last column of y • %delete ALL elements of y

  22. III.3. Augmentation • Augmentation - “Adding to” - making an array bigger. • For example: V = [1, 2, 3]; • To augment more columns, it’s much like doing a running total or running product: to the current variable, perform an action: x = x + 5; %running total on scalar x V = [V, -5, 4.5, 0]; %augmenting v

  23. III.3. Augmentation • Augmenting with other vectors: V1 = [3, 4, 5]; V2 = [6, 7, 8]; V1 = [V1; V2]; • Augmenting with column vectors: V1 = [6; 8; 9]; V2 = [10; 20; 30]; V1 = [V1, V2]; Result: Makes a matrix! __ __ __. Result: __ __ __. __ __ . Why use a comma? ________________ __ __ __ __

  24. III.3. Augmentation • Works for matrices, too!! M1 = [1, 2, 3; 4, 5, 6]; %original matrix M1 = [M1; 7, 8, 9]; % attach a row to M1 M1 = [M1, [11, 2, 33; 44, 33, 22; 1, 0, 2]] M1 = 1 2 3 11 2 33 4 5 6 44 33 22 7 8 9 1 0 2 • Be sure to augment with the correct number of rows / columns!

  25. IV. Common Functions • Simple operations can be done in one step. • Vectors • Sum all the elements of a vector : sum(v) • Multiply all the elements of a vector: prod(v) • Calculate the average of all the elements of a vector: mean(v) • Matrices • It only operates on each COLUMN. • Do the operation twice to operate on the entire matrix sum(sum(M)) average(v)

  26. Mechanics Reduction Gears How are gears made? (video) http://science.howstuffworks.com/transport/engines-equipment/gear.htm How are they used? (video) http://www.geeksaresexy.net/2011/06/16/amazing-lego-orrery-video/

  27. Mechanics Reduction Gears http://www.thefreedictionary.com

  28. Mechanics Reduction Gears Solve by hand

  29. Lessons Learned • Hardcodingarrays [] , ; : ‘ Prefer these symbols when arrays are small, NOT when arrays are big. Exception: the colon can technically create big arrays instantly. • Referencing • Index = position number • Use one index in vectors vector(index number) • Use two indices in matrices matrix(row, colum) • Common operations • Slicing: concentrating on a piece of an array • Diminution: getting rid of elements. Use =[ ]; • Augmenting: “adding values” – increasing the size of an existing array • Combine any symbols/method, as long as the array created is rectangular! • Common functions • sum() prod() mean() max() min() • Different results when applied to vector compared to matrices Go ahead, use MATLAB and arrays to check your math homework!

More Related