Docsity

Arrays and Array Operations in MATLAB, Study notes of Engineering

Toggle sidebar
bg1
1
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
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Arrays and Array Operations in MATLAB and more Study notes Engineering in PDF only on Docsity!

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.

x=[1 3 5 7 9]

x = 1 3 5 7 9

Y=[0, 3+4i; 1-i,2]*

Y = 0 3.0000 + 4.0000i 1.0000 - 1.0000i 2.

a=1; b=2; c=3; d=4; e=5;, f=6; Z=[a a+b a-c; d e+2f e^b]*

Z = 1 3 -

The power of MATLAB is its ability to process entire arrays as a basic data type

in a single operation.

Example 6.1.

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=5z + 1 % Multiply all elements in array z by 5 and then add 1 to % each element and store result in array z*

z = -14 6 26 46 66

Example 6.1.

theta=[0 pi/4 pi/3 pi/2; pi 5pi/4 4pi/3 3pi/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. - 3.1416 3.9270 4.1888 4.
  • r = 1.0000 0.7071 0.5000 0.
    • -1.0000 -0.7071 -0.5000 -0.

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.

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

B = 10 8 6 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

6.3 Array Construction

For large arrays with linearly spaced values, construction is simplified by the use

of colon notation or the MATLAB function 'linspace'. The syntax for colon

notation is:

x = Initial Value:Increment:Final Value

Example 6.3.

x=0:5:

x = Columns 1 through 12 0 5 10 15 20 25 30 35 40 45 50 55 Columns 13 through 21 60 65 70 75 80 85 90 95 100

y=10:-2:-

y = 10 8 6 4 2 0 -2 -4 -6 -8 -

z=25:0.5:

z = Columns 1 through 7 25.0000 25.5000 26.0000 26.5000 27.0000 27.5000 28. Columns 8 through 11 28.5000 29.0000 29.5000 30.

a=15; t=a:a+

t = 15 16 17 18 19 20

Note, in the last example, the increment defaults to one when only two values are

present. The closing brackets are not required unless the array is part of a larger array.

Example 6.3.

x=[ 1 2 10:2:20 30]

x = 1 2 10 12 14 16 18 20 30

y=[1:10; 0 0 50:2:64]

y = 1 2 3 4 5 6 7 8 9 10 0 0 50 52 54 56 58 60 62 64

The 'linspace' operator is used to create arrays consisting of a specified

number of elements ranging from an initial value to a final value. The syntax is

x=linspace(Initial Value, Final Value, Number of points)

6.4 Array Orientation

Row vectors are arrays with a single row. Column vectors are arrays with a single

column. Column vectors are created directly using the semicolon to assign values to

consecutive rows or alternatively by transposing a single-row array. The transpose

operator is ( ' ).

Example 6.4.

y=[1;2;3;4;5;6;7] % Create a 7 ×× 1 array or column vector

y = 1 2 3 4 5 6 7

x=1:7 % Create row vector x x=x' % Transpose row vector x to column vector x

x = 1 2 3 4 5 6 7

x = 1 2 3 4 5 6 7

Rectangular arrays are transposed with the same operator.

Example 6.4.

A=[1:4;5:8;9:12] % Create 3 ×× 4 array A

B=A' % Transpose A into 4 ×× 3 array B by swapping rows and columns of A

A = 1 2 3 4

B = 1 5 9

For complex arrays, i.e. arrays with complex numbers, the transpose operator

produces the complex conjugates in a transposed array.

Example 6.4.

x=[1+2i, 3i, 5, 4-9i] y=x'*

x = 1.0000 + 2.0000i 0 + 3.0000i 5.0000 4.0000 - 9.0000i

y = 1.0000 - 2.0000i 0 - 3.0000i

4.0000 + 9.0000i

The dot-transpose operator ( .' ) transposes the array but does not conjugate the

complex numbers.

Example 6.4.

x=[12-i; 3; 2+5i;] y=x.'*

x = 12.0000 - 1.0000i

2.0000 + 5.0000i

y = 12.0000 - 1.0000i 3.0000 2.0000 + 5.0000i

x=[1+i,1-i; 2+3i,5-2i; 4i,8+2i] y=x' % Transpose and conjugate x z=x.' % Transpose x**

x = 1.0000 + 1.0000i 1.0000 - 1.0000i 2.0000 + 3.0000i 5.0000 - 2.0000i 0 + 4.0000i 8.0000 + 2.0000i

y = 1.0000 - 1.0000i 2.0000 - 3.0000i 0 - 4.0000i 1.0000 + 1.0000i 5.0000 + 2.0000i 8.0000 - 2.0000i

z = 1.0000 + 1.0000i 2.0000 + 3.0000i 0 + 4.0000i 1.0000 - 1.0000i 5.0000 - 2.0000i 8.0000 + 2.0000i

B=A^

??? Error using ==> ^ Matrix must be square.

C=2^A

??? Error using ==> ^ Matrix must be square.

The correct syntax to achieve the desired results in Example 6.5.3 are likewise

deferred until the next section.

6.6 Array-Array Mathematics

Mathematical operations involving several arrays or matrices of like size can be

performed on an element by element basis. For example if A and B are m × n arrays ( m

and n different) with typical element a ij and b ij, then 'A+B' and 'A-B' produce what

would normally be expected, i.e. m × n matrices equal to the sum and difference of the

given matrices. Suppose an m × n matrix C is required where the typical element c ij is

equal to a ij⋅ b ij. The MATLAB statement 'C = A*B' is incorrect and in fact will not

even execute. The reason is quite simple. MATLAB interprets the expression 'A*B' as

a matrix multiplication and in this case the matrix product of A and B is not defined. If

the indices m and n are equal, i.e. A and B are square matrices, the expression 'A*B' will

result in the correct product matrix, still not the desired result. The dilemma is solved by

inserting a period (.) before the multiplication operator, instructing MATLAB to perform

the element by element operation (multiplication in this example).

Example 6.5.

A=[1 2 3; 4 5 6; 7 8 9; 10 11 12] % A is 4 ×× 3

B=[12 11 10; 9 8 7; 6 5 4; 3 2 1] % B is 4 ×× 3

A = 1 2 3

B = 12 11 10

C=A*B

??? Error using ==> * Inner matrix dimensions must agree.

C=A.B*

C = 12 22 30

Constructing the matrix C where c ij = a ij/ b ij is achieved in a similar way, namely

by using the './' operator to specify the element by element operations required to

achieve the desired result.

6.7 Standard Arrays

Certain arrays occur often enough to justify a simpler process for creating them.

For example, arrays with all ones or all zeros are created using the 'ones' and

'zeros' functions. The following example illustrates their use.

Example 6.7.

A=ones(2,3) % Create 2 ×× 3 array of all 1's

A = 1 1 1

y=zeros(1,5) % Create 1 ×× 5 array of all 0's

y = 0 0 0 0 0

B=ones(4) % Create 4 ×× 4 array of all 1's

B = 1 1 1 1

C=zeros(3) % Create 3 ×× 3 array of all 0's

C = 0 0 0 0 0 0 0 0 0

D=[ones(3) zeros(3); zeros(3) ones(3)]% Create 6 ×× 6 array with sub-arrays

D = 1 1 1 0 0 0

The identity matrix and others similar to it, but not square, are easily formed.

Example 6.7.

I=eye(3) % Create 3 ×× 3 Identity matrix

I24=eye(2,4) % Create 2 ×× 4 array with embedded 2 ×× 2 Identity matrix

I42=eye(4,2) % Create 4 ×× 2 array with embedded 2 ×× 2 Identity matrix

I = 1 0 0

I24 = 1 0 0 0

I42 = 1 0

Arrays of random numbers uniformly distributed from 0 to 1 and standard normal

numbers are constructed with MATLAB functions 'rand' and 'randn'.

Example 6.7.

x=rand(1,7) % Create 1 ×× 7 row vector of 7 U(0,1) random numbers y=10+(15-10)x % Transform x to array of 7 U(10,15) random numbers*

x = 0.1341 0.0653 0.3751 0.3735 0.4840 0.9695 0. y = 10.6704 10.3266 11.8757 11.8676 12.4201 14.8473 11.

A=rand(4) % Create 4 ×× 4 array of U(0,1) random numbers

A = 0.2527 0.4864 0.8578 0. 0.5849 0.4961 0.6098 0. 0.5237 0.8432 0.5657 0. 0.1634 0.8062 0.6119 0.

y=randn(10,1) % Create 10 ×× 1 column vector of N(0,1) Normally % distributed random numbers z=50+3y % Transform y to 10 ×× 1 column vector of N(50,3) Normally % distributed random numbers*

y = -0. -1.

-1.

-0.

z = 48.

6.8 Array Manipulation

Arrays are modified and manipulated by addressing the appropriate cells and

inserting the desired values. The follwing examples demonstrate array manipulation in

MATLAB.

Example 6.8.

clear A(3,3)=0 % Create 3 ×× 3 array A and fill with 0's A(2,2)=5 % Change element A(2,2) to 5 A(1,3)=-1 % Change element A(1,3) to - A(4,5)=10 % Expand A to a 4×5 array and set value of A(4,5) to 10

A = 0 0 0 0 0 0 0 0 0

A = 0 0 0

A = 0 0 -

A = 0 0 -1 0 0

Note, if array 'A' does not currently exist in the MATLAB Workspace, the

assignment 'A(3,3) = 0' creates a 3× 3 array with all zeros. The last statement

'A(4,5)=10' expands the original 3×3 array in size to a 4×5 array with extra zeros

inserted and sets the A(4,5) element to 10.

The colon is an effective way to create or modify entire rows and columns (or

parts thereof). The line 'A(3,:)=0' in the example below is equivalent to

'A(3,1:5)=0'.

Example 6.8.

A=diag(0:4) % Create a 5 ×× 5 diagonal array with elements 0,1,2,3, A(3,:)=0 % Set the 3rd row of A equal to all zeros A(2:4,5)=-8 % Set A(2,5), A(3,5) and A(4,5) equal to -

A = 0 0 0 0 0

A = 0 0 0 0 0

A = 0 0 0 0 0

B=[1:5;6:10;11:15;16:20;21:25] % Create 5 ×× 5 array B B(2:3,4:5)=0 % Set elements B(2,4), B(2,5), B(3,4), B(3,5) to zero

B = 1 2 3 4 5

B = 1 2 3 4 5

C=B(:,[1 3 5]) % Set C equal to 5 ×× 3 array made up of the 1st, 3rd % and 5th columns of array B

C = 1 3 5

D=C(3:4,2:3) % Set D equal to 2 ×× 2 array using elements in 3rd and % 4th rows, and 2nd and 3rd columns of C

D = 13 0

E=C(:) % Create a 15 ×× 1 array E made up of the 3 columns of array C

E = 1

Example 6.8.

A=[1:4;8:-1:5; 0 -3 9 12] % Create 3×4 array A x=A(3) % Finds the A(3,1) element of array A y=A(7) % Finds the A(1,3) element of A z=A(end) % Finds the A(3,4) element of array A u=A(5:10) % Finds elements A(2,2),A(3,2),A(1,3),A(2,3),A(3,3),A(1,4)

A = 1 2 3 4

x = 0

y = 3

z = 12

u = 7 -3 3 6 9 4

A new data type in MATLAB called a logical array can be an effective way to

adddress elements of an array that have a common property.

Example 6.8.

x=100+10randn(1,7) % Generate array of random numbers ~ N(100,10) xgt98=x>98 % Create logical array xgt98 same size as x with 1's (True) % in positions corresponding to wherever element in x > % and 0's (False) where element in x <= y=x(xgt98) % Find the elements in array x which are > 98*

x = 112.5400 84.0627 85.5904 105.7115 96.0011 106.9000 108. xgt98 = 1 0 0 1 0 1 1 y = 112.5400 105.7115 106.9000 108.

Logical arrays differ from numeric arrays and can be used to address other arrays

of the same size while numeric arrays consisting of 1's and 0's cannot.

Example 6.8.

x=1:2:15 % Create 1 ×× 8 array x a=[2 5 7] % Create index vector 'a' w=x(a) % Address x using index vector 'a' and store in 1 by 3 array w

x = 1 3 5 7 9 11 13 15 a = 2 5 7 w = 3 9 13

x36=(x>=3 & x<=6)% Create 1 ×× 8 logical array 'x36' with 1's where % elements in x are betwen 3 and 6 and 0's elsewhere y=x(x36) % Find elements (numerical values) in x between 3 and 6

x36 = 0 1 1 0 0 0 0 0 y = 3 5

b=[1 0 1 0 1 0 0 0] % Create 1 ×× 8 numerical array b x=0:5: %z=x(b) % Invalid MATLAB statement because b is not a logical array Lb=logical(b) % Convert numerical array b to a logical array Lb z=x(Lb) % Extracts the 1st, 3rd and 5th elements of array x

b = 1 0 1 0 1 0 0 0 x = 0 5 10 15 20 25 30 35 Lb = 1 0 1 0 1 0 0 0 z = 0 10 20