Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

Similar presentations


Presentation on theme: "Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)"— Presentation transcript:

1 Array Accessing and Strings ENGR 1187 MATLAB 3

2 Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

3 Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

4 What is Addressing (indexing)?  Each element in a vector has an address, also called an index  MATLAB indexing starts at 1 (not at 0!)  We can access/retrieve/extract the individual elements by referring to their addresses  Useful for transforming data or doing calculations with only part of a vector

5 Recall from the previously class that seismic data is important in structural design for civil engineers. Accessing data from an array at a certain location in California allows engineers to design their structures according to vibrational data in a specific region. This allows the building to be designed to this standard but not overdesigned to more extreme data in other regions. Array Accessing In The Real World

6 Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

7 Vector Addressing Example  Define a vector with 9 elements: >> v = [ 12 15 18 21 24 27 30 33 36]; We can access the elements individually: >> v(4) ans = 21

8 Vector Addressing Example We can retrieve any element by indexing: >> v(7) ans = 30 >> v(9) ans = 36 We can assign individual vector elements to variables: >> B= v(7) B = 30 >> C=v(9) C = 36

9 Vector Addressing Examples We can add elements together. Recall: B = v(7), C = v(9) >> D= B + C D = 66 We can also add elements directly: >> v(4) + v(7) ans = 51

10 Changing Element Values  We can change an element in a vector by directly assigning a new value to a specific address.  Let’s change the 6 th element of v to 90: v= [12 15 18 21 24 27 30 33 36] >> v(6) = 90; >> v v = 12 15 18 21 24 90 30 33 36

11 Addressing Column Vectors Addressing (indexing) an element in a column vector works the same way as with a row vector: >> col = [25; 30; 35; 40; 45; 50] >> t = col(4) t = 40

12 Vector Functions  MATLAB has MANY built-in functions we can use with vectors max() min() sum() length() …etc.

13 Vector Functions Examples length() gives us the number of elements in a vector >> fun = [4 6 8 10 12]; >> length(fun) ans = 5

14 Vector Functions Examples Zeros() gives us a vector or matrix of zeros >> nothing = zeros (1, 7) nothing = 0 0 0 0 0 0 0

15 Vector Functions Examples ones() gives us a vector/matrix of all ones >> single = ones(1, 12) single = 1 1 1 1 1 1

16 Addressing a Range of Elements  The colon operator allows us to access a range of elements in a vector  This is useful if we want to extract or alter only a portion of an existing vector

17 Example: Addressing a Range Define a vector: >> vec = [ 1 3 5 7 9 11 13 15 ]; Select elements 3 through 7 in 'vec': >> vec(3:7) vec = 5 7 9 11 13

18 Example: Addressing a Range We can access a range of elements in any vector and assign them to a new variable. Recall that vec = [ 1 3 5 7 9 11 13 15 ] >> t= vec(2:5) t = 3 5 7 9

19 Vector Modifications We can add elements to any existing vector. Recall that 'vec' has 8 elements: vec = [ 1 3 5 7 9 11 13 15 ] >> vec(9: 12)= [ 2 4 6 8] vec = 1 3 5 7 9 11 13 15 2 4 6 8

20 Vector Modifications We can create new vectors made up of elements from previously defined vectors: >> E = [ 3 6 9 12 ]; >> G = [ 2 4 8 5]; >> K = [ E(1:3) G(3:4)] K = 3 6 9 8 5

21 Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

22 Matrix Addressing  Matrix addressing works very similarly to vector addressing  Individual elements are addressed by their row number and column number: (m, n)

23 Matrix Addressing Example Let's define a matrix, then access some elements: >> data = [ 2 3 4 5 ; 1 6 8 9] data = 2 3 4 5 1 6 8 9 >> data (2,3) ans = 8

24 Matrix Addressing Example We can perform mathematical operation with matrix elements. Let's add two values from our matrix called 'data': data = 2 3 4 5 1 6 8 9 >> data_sum= data(1,2) + data(2,4) data_sum = 12

25 Colon Operator With Matrices  A(:, 3)Elements in all rows of column 3  A(2, : )Elements in all columns of row 2  A(:, 2:5) Elements in columns 2 to 5 in all rows  A(2:4, :) Elements in rows 2 to 4 in all columns  A(1:3, 2:4) Elements in rows 1 to 3 and in columns 2 to 4

26 Extracting Matrix Elements  We can extract a portion of a matrix and assign it to a new variable  new_matrix =matrix( r1 : r2, c1 : c2) r1 is the starting row r2 is the ending row c1 is the starting column c2 is the ending column

27 Example: Extracting Elements >> A = [ 1 3 5 7 2 4 6 8 3 6 9 12 4 8 12 16] A = 1 3 5 7 2 4 6 8 3 6 9 12 4 8 12 16 >> B = A(1:3, 2:4) B = 3 5 7 4 6 8 6 9 12

28 Example: Extracting Elements >> C = A(1:3, : ) C = 1 3 5 7 2 4 6 8 3 6 9 12 >> D = A( :, 2:4) D = 3 5 7 4 6 8 6 9 12 8 12 16 Remember

29 Important Takeaways  An element in a defined vector can be accessed with v(x) - an element in a vector can be defined, or re- defined with v(x)=z  An element in a defined matrix can be accessed with v(x:y)- an element in a matrix can be defined, or re- defined with v(x:y)=z  Strings are lines of text and can be used instead of numerical values - they are defined inside single apostrophes, e.g. ‘Your text here.’

30 Preview of Next Class  Array Operations Scalar – vector operations Vector – vector operations  Dot operator, when to use it Built-in vector functions  Ex: max, min, mean etc. Examples

31 What’s Next?  Review today’s Quiz #03  Open the in-class activity from the EEIC website and we will go through it together.  Then, start working on MAT-03 homework.  Before next class, you will read about array operations, this is an introduction of mathematical operations in MATLAB and basics of linear algebra.


Download ppt "Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)"

Similar presentations


Ads by Google