1 / 12

Array Accessing and Strings in MATLAB

Array Accessing and Strings in MATLAB. Creating Arrays / Chapter 2. Topics Covered: Array addressing. 2. Character arrays. 34- 35. >> v = [35 46 78 23 5 14 81 3 55] v = 35 46 78 23 5 14 81 3 55. >> v(4) ans = 23. >> v(7) ans =

ron
Télécharger la présentation

Array Accessing and Strings in MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Accessing and Strings in MATLAB Creating Arrays / Chapter 2 Topics Covered: • Array addressing. 2. Character arrays.

  2. 34- 35 >> v = [35 46 78 23 5 14 81 3 55] v = 35 46 78 23 5 14 81 3 55 >> v(4) ans = 23 >> v(7) ans = 81 >> v(1) ans = 35 It is possible to change an element in a vector by entering a value to a specific address directly: >> v(6) = 273 v = 35 46 78 23 5 273 81 3 55 ARRAY ADDRESSING (VECTOR) The address of an element in a vector is its position in the row (or column), starting at 1.

  3. 36- 37 Addressing Specified Elements of a Vector Any combination of elements from a vector can be addressed in any order v([4 5 7 3 3]) is the same as( [v(4), v(5), v(7), v(3), v(3)] ). >> v = [4 15 8 12 34 2 50 23 11] v = 4 15 8 12 34 2 50 23 11 >> u = v([4 5 7 3 3]) u = 12 34 50 8 8

  4. 36- 37 USING A COLON (:) WHEN ADDRESSING VECTORS The colon operator can be used to generate a list of elements to address v(:) v(3 : 7) All the elements of a vector (either a row vector or a column vector) Elements 3 through 7 ( [v(3), v(4), v(5), v(6), v(7)] ). >> v = [4 15 8 12 34 2 50 23 11] v = 4 15 8 12 34 2 50 23 11 >> u = v(3:7) u = 8 12 34 2 50

  5. 35 >> m(1,1) ans = 3 >> m(2,3) ans = 10 >> m=[3 11 6 5; 4 7 10 2; 13 9 0 8] m = 3 11 6 5 4 7 10 2 13 9 0 8 Single elements can be used like variables in computations: >> m(2,3) - m(1,1) ans = 7 ARRAY ADDRESSING (MATRIX)

  6. 36- 37 USING A COLON (:) WHEN ADDRESSING MATRICES A(: , 3) A(2 , :) A(: , 2:5) A(2:4, :) A(1:3, 2:4) A([4 2],:) Elements in all the rows of column 3 Elements in all the columns of row 2 Elements in columns 2 through 5 in all the rows Elements in rows 2 through 4 in all the columns Elements in rows 1 through 3 and in columns 2 through 4 Elements in all the columns of rows 4 then 2

  7. 36- 37 ADDRESSING MATRICES Define a matrix using part of A Define a matrix >> B = A(:,3) B = 5 6 9 12 15 >> A = [1 3 5 7 9; 2 4 6 8 10; 3 6 9 12 15; 4 8 12 16 20; 5 10 15 20 25] A = 1 3 5 7 9 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 >> C = A(2,:) C = 2 4 6 8 10

  8. 36- 37 ADDRESSING MATRICES A = 1 3 5 7 9 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 >> E = A(2:4,:) E = 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 >> D = A(:, 2:5) D = 3 5 7 9 4 6 8 10 6 9 12 15 8 12 16 20 10 15 20 25 >> G = A([5 2],5:-2:1) G = 25 15 5 10 6 2 >> F = A(1:3,2:4) F = 3 5 7 4 6 8 6 9 12

  9. 36- 37 ADDRESSING MATRICES >> A = zeros (4,5) A = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> A(1:2,1:3) = B([5 5],2:4) A = 10 15 20 0 0 10 15 20 0 0 0 0 0 0 0 0 0 0 0 0 B = 1 3 5 7 9 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 >> A(3:4,[1 4 5]) = B(1:2,1:3) A = 10 15 20 0 0 10 15 20 0 0 1 0 0 3 5 2 0 0 4 6

  10. 46- 48 Character Arrays • Strings are a special case of a vector (row array) where every element is a character. • Strings are created by entering the characters between single quotes. • String arrays can include letters, digits, other symbols, and spaces • Examples of strings: 'ad ef ', '3%fr2', '{edcba :21!'

  11. 46- 48 STRING VARIABLES >> a = 'ERty 8' a = ERty 8 >> B = ['My name is John Smith'] B = My name is John Smith a has 6 elements, and B has 21 elements The elements can be addressed like any other row array >> a(4) ans = y >> B(12) ans = J

  12. 46- 48 STRING VARIABLES The string variable: >> x = '536' x = 536 >> x = 536 x = 536 is not the same as the number variable: If you use a string variable in calculations, you get an answer based on the ascii storage code for those characters (probably not what you wanted)! An important application of strings is in creating input prompts and output messages. This will be shown later when script file I/O (input/output) is discussed.

More Related