Arrays and Array Operations

Arrays and Array Operations - Chapter 6 Arrays and Array...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6 Arrays and Array Operations 6.1 Simple Arrays Arrays are rectangular entities consisting of rows and columns. Arrays are input a row at a time with individual elements separated by a space or a comma. A new row is begun following a semicolon. The complete set of entries are enclosed between square brackets. Example 6.1.1 x=[1 3 5 7 9] x = 1 3 5 7 9 Y=[0, 3+4*i; 1-i,2] Y = 0 3.0000 + 4.0000i 1.0000 - 1.0000i 2.0000 a=1; b=2; c=3; d=4; e=5;, f=6; Z=[a a+b a-c; d e+2*f e^b] Z = 1 3 -2 4 17 25 The power of MATLAB is its ability to process entire arrays as a basic data type in a single operation. Example 6.1.2 x=[1 3 5 7 9] % Create array x y=x-5 % Subtract 5 from each element in x and store result in array y x = 1 3 5 7 9 y = -4 -2 0 2 4 z=x+y % Add arrays x and y element by element z = -3 1 5 9 13 z=5*z + 1 % Multiply all elements in array z by 5 and then add 1 to % each element and store result in array z 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
z = -14 6 26 46 66 Example 6.1.3 theta=[0 pi/4 pi/3 pi/2; pi 5*pi/4 4*pi/3 3*pi/2] r=cos(theta) % Compute cosine of all elements in array theta and store % result in array r theta = 0 0.7854 1.0472 1.5708 3.1416 3.9270 4.1888 4.7124 r = 1.0000 0.7071 0.5000 0.0000 -1.0000 -0.7071 -0.5000 -0.0000 2
Background image of page 2
6.2 Array Addressing or Indexing Individual elements in an array are addressed by using subscripts to identify them. A single subscript is used for vectors, i.e. arrays with a single row or column. Double subcripts are required for 2 dimensional arrays. Example 6.2.1 a=[0 1 3 6 10] % Create row vector a a(1),a(5) % Display 1 st and 5 th elements of a a = 0 1 3 6 10 ans = 0 ans = 10 b=[0;1;3;6;10] % Create column vector b b(2),b(4) % Display 2 nd and 4 th elements of b b = 0 1 3 6 10 ans = 1 ans = 6 x=a(3)+b(3) y=a(1)-b(5) x = 6 y = -10 C=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15] % Create 3 by 5 array C C(2,3),C(3,5) % Display elements C(2,3),C(3,5) C = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ans = 8 ans = 15 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A block of continuous elements can be addressed using a colon between the first and last elements. Example 6.2.2 x=[1,2,3,4,5; 6,7,8,9,10; 11,12,13,14,15; 16,17,18,19,20] % Create x y=x(1,2:4) % Selects elements x(1,2),x(1,3),x(1,4) x = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 y = 2 3 4 x % Display array x z=x(2:end,3) % Selects elements x(2,3),x(3,3),x(4,3) x = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 z = 8 13 18 Note the array y is 1 × 3 and the array z is 3 × 1 and therefore y+z is an illegal operation. A double colon is used to address non-continuous elements. Example 6.2.3 A=[1 2 3 4 5 6 7 8 9 10;10 9 8 7 6 5 4 3 2 1;0 2 4 6 8 10 12 14 16 18] B=A(2,1:2:7) % Selects elements A(2,1),A(2,3),A(2,5),A(2,7) A = 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 0 2 4 6 8 10 12 14 16 18 B = 10 8 6 4 4
Background image of page 4
A % Display A C=A(3,1:3:10) % Selects elements A(3,1),A(3,4),A(3,7),A(3,10) A = 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 0 2 4 6 8 10 12 14 16 18 C = 0 6 12 18 A % Display A D=A(1,10:-2:4) % Selects elements A(1,10),A(1,8),A(1,6),A(1,4) A = 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1 0 2 4 6 8 10 12 14 16 18 D = 10 8 6 4 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6.3
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

Arrays and Array Operations - Chapter 6 Arrays and Array...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online