350 likes | 475 Views
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.
E N D
ArraysCommon Opeartions Slicing Diminution Augmentation 1
Accessingmore 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. % Copy all elements in rows 1 and 2, % columns 1 through 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(___ ,____);
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 ,____);
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 ,____);
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);
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);
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);
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!
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!
Making arrays smaller Deleting an element, a row, a column, etc.. ARRAY DIMINUTION Pronounce: “Dim’ – min – yoo’ – shun” 12
Array Diminution • To eliminate the wholecontent, re-define it as an empty-vector: scores = []; %delete all scores
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
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
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.
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.
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
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
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
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!”
Array Diminution, cont. Question: • Can we eliminate a single value from a matrix? M = [1, 2, 3; 4, 5, 6]; M(2, 2) = [] <enter>
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.
Real life#2 – similar example Clearly, bad results on the walls…
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? ____________________________________
Insert values at the end of an array (not in the middle, nor beginning) Augmenting an array 28
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
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
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
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
Extending an array Array b does not have 4 columns… mmm… what will it do?
Extending an array Fills in with zerossss.
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.