Upload
kort
View
32
Download
0
Tags:
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
1
1. Slicing2. Diminution3. Augmentation
ArraysCommon Opeartions
1
ARRAY SLICINGAccessing more than one element of an array
22
Array SlicingIn 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
3
…
… …
Array SlicingIn 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(___ ,____);
4
Array SlicingIn 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 ,____);
5
Array SlicingIn 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 ,____);
6
Array SlicingIn 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);
7
Array SlicingIn 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);
8
Array SlicingIn 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);
9
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!
10
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!
11
ARRAY DIMINUTION
Making arrays smallerDeleting an element, a row, a column, etc..
1212
Pronounce:“Dim’ – min – yoo’ – shun”
Array Diminution To eliminate the whole content, re-define it as an empty-
vector:scores = []; %delete all scores
13
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
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
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:
16
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:
17
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:
18
%”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:
19
%”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:
20
%”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:
21
%”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>
22
Array Diminution, cont.Question:
Can we eliminate a single value from a matrix?
M = [1, 2, 3; 4, 5, 6];M(2, 2) = [] <enter>
23
No – because that would mean some rows or columns would have more values than others.
Real life#2 – similar example
24
Clearly, bad results on the walls…
27
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)
2828
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
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
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
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
Extending an array
33
Array b does not have 4 columns… mmm… what will it do?
Extending an array
34
Fills in with zerossss.
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