array referencing 1 1. ii. array referencing assume an array has values. it is useful to “refer...

36
ARRAY REFERENCING 1 1

Upload: aleesha-sandra-rodgers

Post on 11-Jan-2016

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

ARRAY REFERENCING

11

Page 2: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

2

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

Page 3: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

3

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

Page 4: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

4

Index

This process of repeatedly adding numbers to a single variable is called a “running sum”

Page 5: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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…

5

Page 6: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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)

6

Row number always first!

Column number always second!

Page 7: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

7

Row number always first!

Column number always second!

42

Page 8: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

8

Row number always first!

Column number always second!

42

Page 9: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

99

The same can be done with columns!

V = M(2, :); % V gets a copy of all columns of row 2

Page 10: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

ARRAY SLICINGAccessing more than one element of an array

1010

Page 11: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

11

… …

Page 12: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

12

Page 13: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

13

Page 14: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

14

Page 15: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

15

Page 18: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

ARRAY DIMINUTION

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

1818

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

Page 19: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

19

Page 20: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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:

20

This action changes the original vector and cannot be undone.

Page 21: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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:

21

This action changes the original vector and cannot be undone.

Page 22: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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:

22

%”M

Page 23: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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:

23

%”M , all-rows

Page 24: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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:

24

%”M , all-rows, 1stcolumn

Page 25: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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:

25

%”M , all-rows, 1stcolumn , delete!”

Page 26: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

Array Diminution, cont.

Question:

• Can we eliminate a single value from a matrix?

M = [1, 2, 3; 4, 5, 6];M(2, 2) = [] <enter>

26

Page 27: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

Array Diminution, cont.

Question:

• Can we eliminate a single value from a matrix?

M = [1, 2, 3; 4, 5, 6];M(2, 2) = [] <enter>

27

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

Page 28: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

2828

Page 29: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

29

Result: [ ___________________ ] ?

29

1, 2, 3 , 4, 5, 6

Page 30: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

30

Makes a matrix!

Result:__ __ __.

__ __ __.

30

1, 2, 3 , 4, 5, 6Result: [ ___________________ ] ?

3, 4, 5

6, 7, 8

Page 31: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

31

Makes a matrix!

Why use a comma? ________________

Result:__ __ __.

__ __ __.

Result:__ __ .

__ __ __ __

31

Result: [ ___________________ ] ?1, 2, 3 , 4, 5, 6

3, 4, 5

6, 7, 8

68

9

1020

30

Page 32: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

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

32

Be sure to augment with the correct number of rows / columns!

32

Page 33: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

Extending an array

33

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

Page 34: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

Extending an array

34

Fills in with zeros.

Page 35: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

35

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!

Page 36: ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions

36

Helpful functions

• How many elements are in the array?

Function Return value

length(vector) Number of elements in the vector

length(matrix) Highest dimension

size(matrix,1) Number of rows in the matrix

size(matrix,2) Number of columns in the matrix

size(matrix) 1 by 2 array of row and column dimensions

numel(array) Number of elements total in the array