1 arrays 2.common operations 1. slicing 2. diminution 3. augmentation 1

17
1 Arrays 2. Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

Upload: cordelia-dean

Post on 28-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

1

Arrays

2. Common Operations1. Slicing2. Diminution3. Augmentation

1

Page 2: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

ARRAY SLICINGUsed for:

Accessing more than one element of an arrayEliminating bad elements

22

Page 3: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

Array Slicing

In general, when we “pull out” part of an array, we call that a “slice”. The range operator is frequently used when getting a slice.

% Pull out all elements in rows 1 and 2

% that are in columns 1 through 4

M1 = M(1:2, 1:4);

3

Page 4: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

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!

4

Page 5: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

ARRAY DIMINUTION

Making arrays smaller

Deleting an element

Deleting a row

Deleting a column

55

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

Page 6: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

Array Diminution

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

scores = []; %delete all scores

To eliminate a single value from a vector, you can either take a slice:

HighScores = [757, 65, -13, -89];HighScores = HighScores(1:3); %deletes last

%score

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

6

Page 7: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

Example Diminution After analyzing data, you may wish to get rid of some data: in this

case, assign the empty brackets []

For example, get rid of the number 8 in b below:

7

This action changes the original vector and cannot be undone.

Page 8: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

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:

QUESTION: Can we eliminate a single value from a matrix? M(2,2) = [] ?

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

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

Page 9: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

Real life#2 – similar example

9

Clearly, bad results on the walls…

Page 10: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

10

Real life#2 – similar example

Page 11: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

11

Real life#2 – similar example

Page 12: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

12

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

Page 13: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

AUGMENTING AN ARRAY

1313

Page 14: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

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 did we use a comma? ________________

Result: _ [ _________________ ] _?

Result:

__ __ __. __ __ __.

Result:

__ __ .

__ __ __ __

14

Page 15: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

Array Augmentation, reviewWorks 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 that you augment with the correct number of rows / columns!

15

Page 16: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

Extending an array

16

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

Page 17: 1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1

End of Presentation