1 / 52

Arrays – Part2

Arrays – Part2. Hard-coding vectors and matrices (review) Referencing Common Operations: 1. Slicing 2. Diminution 3. Augmenting. I. Hardcoding Review – The Symbols. [] are used to hardcode values into an array _________ are optional, but they separate COLUMNS

ryder-watts
Download Presentation

Arrays – Part2

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 Hard-coding vectors and matrices (review) Referencing Common Operations: 1. Slicing 2. Diminution 3. Augmenting

  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. ARRAY Referencing 4

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

  6. 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!)

  7. Array Referencing, cont. Refer to the positions using “dimensions”. How many dimensions make up a variable? • A scalar has no dimensions – simply refer to the variable itself. x

  8. Array Referencing, cont. Refer to the positions using “dimensions”. How many dimensions make up a variable? • A scalar has no dimensions – simply refer to the variable itself. x • A vector has one dimension – either a number of rows or a number of columns. Use a single number to reference the values in a vector. ages(3)

  9. Array Referencing, cont. Refer to the positions using “dimensions”. How many dimensions make up a variable? • A scalar has no dimensions – simply refer to the variable itself. x • A vector has one dimension – either a number of rows or a number of columns. Use a single number to reference the values in a vector. ages(3) • A matrix has two or more dimensions. Use a number for EACH dimension: a row number, AND a columnnumber (More dimensions? Use another number for each additional dimension!) pressures(3,56)

  10. Array Referencing - Vectors • Vectors use a single value. Each value is called an “index”: x = [5; -1; 4]; %original vector sum = 0; %start sum at zero sum = sum + x(1); %add first element sum = sum + x(2); %add second element sum = sum + x(3); %add third element

  11. Array Referencing - Vectors • Vectors use a single value. Each value is called an “index”: x = [5; -1; 4]; %original vector sum = 0; %start sum at zero sum = sum + x(1); %add first element sum = sum + x(2); %add second element sum = sum + x(3); %add third element • Vectors have one dimension, so use a single index in parentheses to specify which element to use. Indexing starts at 1, and can go as high as how-many-elements-there-are. Yes, it seems quite repetitive… wouldn’t a loop make it easier? Hang in there…

  12. Array Referencing - Matrices • Matrices are similar. To access the 6 in this matrix: M = [1, 2, 3; 4, 5, 6; 7, 8, 9] Use :M(2,3) Row number alwaysfirst! Column number always second!

  13. Array Referencing - Matrices • Matrices are similar. To access the 6 in this matrix: M = [1, 2, 3; 4, 5, 6; 7, 8, 9] Use :M(2,3) • It can be used directly: x = 7 * M(2,3); %Result? _____ Row number alwaysfirst! Column number always second!

  14. Array Referencing - Matrices • Matrices are similar. To access the 6 in this matrix: M = [1, 2, 3; 4, 5, 6; 7, 8, 9] Use :M(2,3) • It can be used directly: x = 7 * M(2,3); %Result? _____ • The row and column positions specified in the parentheses are referred to as “indices” (plural of “index”): 2 is the “row index” and 3 is the “column index”. Row number alwaysfirst! Column number always second!

  15. Referencing • To refer to “all” of a column or row, use the range operator by itself: V = M(:, 3); %from M, copy all rows in columns 3 to V The same can be done with columns! V = M(2, :); % V gets a copy of all columns of row 2 15

  16. Max, Min – [value, location] • The functions max() and min() can be used to identify the value and the location in the array: [min_value location] = min(array1) [max_value location] = max(array1) mat = mat =

  17. Example: Fuel tank design A fuel tank is to be constructed that will hold 5 x 105 L. The shape is cylindrical with a hemisphere top and a cylindrical midsection. Costs to construct the cylindrical portion will be $300/m2 of surface area and $400/m2 of surface area of the hemispheres. What is the tank dimension that will result in the lowest dollar cost?

  18. Example: Fuel tank design • Solution Steps Calculate minimum cost with respect to radius MATLAB can be used to calculate minimum cost • Plot the data - plot(R,Cost) • Identify minimum for an array of costs - min(Cost) • Numerical Methods (Iterative solutions)

  19. Accessingmore than one element of an array ARRAY SLICING 19

  20. 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, % columns 1 through 4

  21. 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, % columns 1 through 4 M1 = M(___ ,____);

  22. 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, % columns 1 through 4 M1 = M(1:2 ,____);

  23. 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, % columns1 through 4 M1 = M(1:2 ,____);

  24. 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, % columns1 through 4 M1 = M(1:2,1:4);

  25. 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);

  26. 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);

  27. Real-life #1: Eliminating bad 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!

  28. Real-life #1: Eliminating bad 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!

  29. Making arrays smaller Deleting an element, a row, a column, etc.. ARRAY DIMINUTION Pronounce: “Dim’ – min – yoo’ – shun” 29

  30. Array Diminution • To eliminate the wholecontent, re-define it as an empty-vector: scores = []; %delete all scores

  31. Array Diminution • To eliminate the wholecontent, re-define it as an empty-vector: scores = []; %delete all scores • To eliminate a single value from a vector, either take aslice: HighScores = [757, 65, -13, -89]; HighScores = HighScores(1:3); %deletes last %score

  32. Array Diminution • To eliminate the wholecontent, re-define it as an empty-vector: scores = []; %delete all scores • To eliminate a single value from a vector, either take aslice: HighScores = [757, 65, -13, -89]; HighScores = HighScores(1:3); %deletes last %score Oruse the empty-vector: HighScores(4) = []; %removes 4th score

  33. Example Diminution • After analyzing data, get rid of some data: in this case, assign the empty brackets [] • For example, get rid of the number 8 in b below: This action changes the original vector and cannot be undone.

  34. Example Diminution • After analyzing data, get rid of some data: in this case, assign the empty brackets [] • For example, get rid of the number 8 in b below: This action changes the original vector and cannot be undone.

  35. Array Diminution, cont. • To eliminate an entire row/column: • Use the range operator, combined with • the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: %”M

  36. Array Diminution, cont. • To eliminate an entire row/column: • Use the range operator, combined with • the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: %”M , all-rows

  37. Array Diminution, cont. • To eliminate an entire row/column: • Use the range operator, combined with • the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: %”M , all-rows , 1stcolumn

  38. Array Diminution, cont. • To eliminate an entire row/column: • Use the range operator, combined with • the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: %”M , all-rows , 1stcolumn , delete!”

  39. Array Diminution, cont. Question: • Can we eliminate a single value from a matrix? M = [1, 2, 3; 4, 5, 6]; M(2, 2) = [] <enter>

  40. Array Diminution, cont. Question: • Can we eliminate a single value from a matrix? M = [1, 2, 3; 4, 5, 6]; M(2, 2) = [] <enter> No – because that would mean some rows or columns would have more values than others.

  41. Real life#2 – similar example Clearly, bad results on the walls…

  42. Real life#2 – similar example

  43. Real life#2 – similar example

  44. Real life#2 – similar example Suppose you want to delete the top now, since that is also a wall in the wind tunnel. What would be the command line? ____________________________________

  45. Insert values at the end of an array (not in the middle, nor beginning) Augmenting an array 45

  46. Array Augmentation, review 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: V = [V, 4, 5, 6]; Result: [ _________________ ] ? 46

  47. Array Augmentation, review 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: V = [V, 4, 5, 6]; To augment with another row vector variable: V1 = [3, 4, 5]; V2 = [6, 7, 8]; V1 = [V1; V2]; Result: [ _________________ ] ? Result: __ __ __. Makes a matrix! __ __ __. 47

  48. Array Augmentation, review 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: V = [V, 4, 5, 6]; To augment with another row vector variable: V1 = [3, 4, 5]; V2 = [6, 7, 8]; V1 = [V1; V2]; To augment with a column vector variable: V1 = [6; 8; 9]; V2 = [10; 20; 30]; V1 = [V1, V2]; Result: [ _________________ ] ? Result: __ __ __. Makes a matrix! __ __ __. Result: __ __ . Why use a comma? ________________ __ __ __ __ 48

  49. Array Augmentation, review • 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! 49

  50. Extending an array Array b does not have 4 columns… mmm… what will it do?

More Related