1 / 35

Arrays Common Opeartions

Arrays Common Opeartions . Slicing Diminution Augmentation. 1. Accessing more than one element of an array. ARRAY SLICING. 2. Array Slicing. In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice.

kort
Télécharger la présentation

Arrays Common Opeartions

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. ArraysCommon Opeartions Slicing Diminution Augmentation 1

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

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

  4. 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(___ ,____);

  5. 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 ,____);

  6. 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 ,____);

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

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

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

  10. 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!

  11. 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!

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

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

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

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

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

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

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

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

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

  21. 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!”

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

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

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

  25. Real life#2 – similar example

  26. Real life#2 – similar example

  27. 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? ____________________________________

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

  29. 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: [ _________________ ] ? 29

  30. 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! __ __ __. 30

  31. 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? ________________ __ __ __ __ 31

  32. 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! 32

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

  34. Extending an array Fills in with zerossss.

  35. Wrapping Up • Vocabulary: slicing, range operator, diminution, empty vector, empty brackets, augmentation • To slice means to refer to a piece of an array • To copy their values, to replace their values, to delete their values, etc… • Use the : operator to refer to ALL rows or ALL columns. • To diminute an array is to reduce its size • Use empty brackets • Feasible as long as dimension-wise, it makes sense! • To augment an array is to 'add on values' • Only at the beginning or end, not in the middle. • Use [ ] as if creating arrays, use the variable inside the [ ] as well.

More Related