52
Arrays – Part2 I. Hard-coding vectors and matrices (review) II.Referencing III.Common Operations: 1. Slicing 2. Diminution 3. Augmenting 1

Arrays – Part2

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

1

Arrays – Part2

I. Hard-coding vectors and matrices (review)II. ReferencingIII. 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.

ARRAY REFERENCING

44

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

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

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

– 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)

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)

– A matrix has two or more dimensions. Use a number for EACH dimension: a row number, AND a column number (More dimensions? Use another number for each additional dimension!) pressures(3,56)

9

Array Referencing - Vectors

• Vectors use a single value. Each value is called an “index”:x = [5; -1; 4]; %original vectorsum = 0; %start sum at zerosum = sum + x(1); %add first elementsum = sum + x(2); %add second

elementsum = sum + x(3); %add third element

10

Array Referencing - Vectors

• Vectors use a single value. Each value is called an “index”:x = [5; -1; 4]; %original vectorsum = 0; %start sum at zerosum = sum + x(1); %add first elementsum = sum + x(2); %add second

elementsum = 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…

11

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)

12

Row number always first!

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? _____

13

Row number always first!

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

14

Row number always first!

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

15

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

5. Solution StepsCalculate 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)

Example: Fuel tank design

ARRAY SLICINGAccessing more than one element of an array

1919

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

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 4M1 = M(___ ,____);

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

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

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% that are in columns 1 through 4M1 = 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 4M1 = M(1:2, 1:4);

26

ARRAY DIMINUTION

Making arrays smallerDeleting an element, a row, a column, etc..

2929

Pronounce:“Dim’ – min – yoo’ – shun”

Array Diminution

• To eliminate the whole content, re-define it as an empty-vector:

scores = []; %delete all scores

30

Array Diminution

• To eliminate the whole content, re-define it as an empty-vector:

scores = []; %delete all scores

• To eliminate a single value from a vector, either take a slice:HighScores = [757, 65, -13, -89];HighScores = HighScores(1:3); %deletes last

%score

31

Array Diminution

• To eliminate the whole content, re-define it as an empty-vector:

scores = []; %delete all scores

• To eliminate a single value from a vector, either take a slice:HighScores = [757, 65, -13, -89];HighScores = HighScores(1:3); %deletes last

%score

Or use the empty-vector:HighScores(4) = []; %removes 4th score

32

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:

33

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:

34

This action changes the original vector and cannot be undone.

Array Diminution, cont.

• To eliminate an entire row/column:1. Use the range operator, combined with2. the empty-vector

M = [1, 2, 3; 4, 5, 6];M(:, 1) = [] … Read it as:

35

%”M

Array Diminution, cont.

• To eliminate an entire row/column:1. Use the range operator, combined with2. the empty-vector

M = [1, 2, 3; 4, 5, 6];M(:, 1) = [] … Read it as:

36

%”M , all-rows

Array Diminution, cont.

• To eliminate an entire row/column:1. Use the range operator, combined with2. the empty-vector

M = [1, 2, 3; 4, 5, 6];M(:, 1) = [] … Read it as:

37

%”M , all-rows, 1stcolumn

Array Diminution, cont.

• To eliminate an entire row/column:1. Use the range operator, combined with2. the empty-vector

M = [1, 2, 3; 4, 5, 6];M(:, 1) = [] … Read it as:

38

%”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>

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

No – because that would mean some rows or columns would have more values than others.

Real life#2 – similar example

41

Clearly, bad results on the walls…

Real life#2 – similar example

42

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

AUGMENTING AN ARRAYInsert values at the end of an array (not in the middle, nor beginning)

4545

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

4646

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];

Makes a matrix!

Result: [ _________________ ] ?

Result:__ __ __.

__ __ __.

4747

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];

Makes a matrix!

Why use a comma? ________________

Result: [ _________________ ] ?

Result:__ __ __.

__ __ __.

Result:__ __ .

__ __ __ __

4848

Array Augmentation, review

• Works for matrices, too:

M1 = [1, 2, 3; 4, 5, 6]; %original matrixM1 = [M1; 7, 8, 9]; % attach a row to M1M1 = [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!

4949

Extending an array

50

Array b does not have 4 columns… mmm… what will it do?

Extending an array

51

Fills in with zerossss.

52

Key Ideas

I. Hardcoding arrays [] , ; : ‘Prefer these symbols when arrays are small, NOT when arrays are big.

Exception: the colon can technically create big arrays instantly.

II. Referencing• Index = position number• Use one index in vectors vector(index number)• Use two indices in matrices matrix(row, colum)

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

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