35
1. Slicing 2. Diminution 3. Augmentation Arrays Common Opeartions 1

Arrays Common Opeartions

  • Upload
    kort

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 14: Arrays Common Opeartions

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

14

Page 15: Arrays Common Opeartions

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

15

Page 29: Arrays Common Opeartions

Array Augmentation, reviewAugmentation = “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: [ _________________ ] ?

2929

Page 30: Arrays Common Opeartions

Array Augmentation, reviewAugmentation = “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:__ __ __. __ __ __.

3030

Page 31: Arrays Common Opeartions

Array Augmentation, reviewAugmentation = “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:__ __ .

__ __ __ __

3131

Page 32: Arrays Common Opeartions

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!

3232

Page 35: Arrays Common Opeartions

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