520 likes | 604 Views
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
E N D
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 • _______________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
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.
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
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!)
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
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)
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)
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
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…
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!
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!
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!
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
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 =
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?
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)
Accessingmore than one element of an array ARRAY SLICING 19
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” 29
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 45
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
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
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
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
Extending an array Array b does not have 4 columns… mmm… what will it do?