Download presentation
Presentation is loading. Please wait.
Published byAleesha Sandra Rodgers Modified over 9 years ago
1
ARRAY REFERENCING 1 1
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. 2 ?… ?… 3 RD
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!) 3
4
Array Referencing - Vectors Vectors use a single value. Each value is called an “index”: x = [5; -1; 4]; %original vector sum = 0; %start sum at zero sum = sum + x(1); %add first element sum = sum + x(2); %add second element sum = sum + x(3); %add third element 4 Index This process of repeatedly adding numbers to a single variable is called a “running sum”
5
Array Referencing - Vectors Vectors use a single value. Each value is called an “index”: x = [5; -1; 4]; %original vector sum = 0; %start sum at zero sum = sum + x(1); %add first element sum = sum + x(2); %add second element sum = 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
6
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!
7
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
8
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
9
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 9 9 The same can be done with columns! V = M(2, :); % V gets a copy of all columns of row 2
10
ARRAY SLICING Accessing more than one element of an array 10
11
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 … ……
12
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 M1 = M(___,____); 12
13
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 M1 = M(1:2,____); 13
14
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 M1 = M(1:2,____); 14
15
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 M1 = M(1:2,1:4); 15
16
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! 16
17
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! 17
18
ARRAY DIMINUTION Making arrays smaller Deleting an element, a row, a column, etc.. 18 Pronounce: “Dim’ – min – yoo’ – shun”
19
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 4 th score 19
20
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.
21
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.
22
Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 22 %”M
23
Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 23 %”M, all-rows
24
Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 24 %”M, all-rows, 1 st column
25
Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 25 %”M, all-rows, 1 st column, delete!”
26
Array Diminution, cont. Question: Can we eliminate a single value from a matrix? M = [1, 2, 3; 4, 5, 6]; M(2, 2) = [] 26
27
Array Diminution, cont. Question: Can we eliminate a single value from a matrix? M = [1, 2, 3; 4, 5, 6]; M(2, 2) = [] 27 No – because that would mean some rows or columns would have more values than others.
28
AUGMENTING AN ARRAY Insert values at the end of an array (not in the middle, nor beginning) 28
29
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
30
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, 6 Result: [ ___________________ ] ? 3, 4, 5 6, 7, 8
31
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 689689 10 20 30
32
Array Augmentation, review Works for matrices, too: M1 = [1, 2, 3; 4, 5, 6]; %original matrix M1 = [M1; 7, 8, 9]; % attach a row to M1 M1 = [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
33
Extending an array 33 Matrix b does not have 4 columns… mmm… what will it do?
34
Extending an array 34 Fills in with zeros.
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! 35
36
Helpful functions How many elements are in the array? 36 FunctionReturn 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.