Lecture 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

L E C T U R E 2 : A R R AY S

1
2

CONTENTS

Arrays
Two-dimensional Arrays
Element-by-element Operations
Matrix Operations
Polynomial Operations Using Arrays
Practical Exercises
3

V E C T O R S A N D M AT R I C E S

 The array is the fundamental form of Matlab, uses to store data.


 Scalars: one row and one column (special case)
 Vectors:
 Row : one row and multiple columns
 Column: multiple rows and one column
 Matrices: multiple rows and multiple columns
4

A R R AY S O F N U M B E R S
5

ENTERING VECTORS (I)

 In Matlab, a vector, or any list of numbers, can be entered in a horizontal (row) or vertical

(column) vectors.
 Example:

 The data from the previous slide can be entered in rows:

[1984 1986 1988 1990 1992 1994 1996]


[127 130 136 145 158 178 211]
 or in column:
6

ENTERING VECTORS (II)

 A vector is created by typing the elements (numbers) Inside square brackets [ ].


 To create a ROW VECTOR, type a space or a comma between the elements inside the square

brackets.
 NOTE: Matlab is not “picky” about how the data is typed in. Spaces can be typed before and/or

after the sign “=“.


 Between the elements you can have a space in addition to the comma, or type more than one

space.
7

ENTERING VECTORS (IV)


To create a COLUMN VECTOR, type a left
bracket [, and Then enter the elements with a
semicolon between them, or press <ENTER> after
each element. Type a right bracket ] after the last
element.
8

ENTERING VECTORS (V)


CREATING VECTORS WITH CONSTANT
SPACING
 Two common methods:

1. Specify first term : step size : last term


2. linspace (first term, last term, number of
terms)
9

ENTERING VECTORS (VII)


10

T W O D I M E N S I O N A L A R R AY S : M A T R I C E S
11

C R E AT I N G A M AT R I X
12

T H E T R A N S P O S E O P E R AT I O N ( I )
13

T H E T R A N S P O S E O P E R AT I O N ( I I )
14

A R R AY A D D R E S S I N G ( V E C T O R S ) ( I )
15

A R R AY A D D R E S S I N G ( V E C T O R S ) ( I I )
16

A R R AY A D D R E S S I N G ( M AT R I C E S ) ( I )
17

A R R AY A D D R E S S I N G ( M A T R I C E S ) (II)
18

A R R AY A D D R E S S I N G ( M A T R I C E S ) ( I I I )
19

A R R AY A D D R E S S I N G ( M AT R I C E S ) ( I V )
20

S O M E U S E F U L A R R AY F U N C T I O N S ( I )
Command Description
cat(n,A,B,C,…) Creates a new array by concatenating the arrays A,B,C and so on along the dimension
n.
find(x) Computes an array containing the indices of the nonzero elements of the array x.
Computes the arrays u and v, containing the row and column indices of the nonzero
[u,v,w]=find(A) elements of the matrix A, and the array w, containing the values of the nonzero
elements. The array w may be omitted.
Computes either the number of elements of A if A is a vector or the largest value of m
or n if A is an mxn matrix.
length (A)
Creates a row vector of n regularly spaced values between a and b.
Creates a row vector of n logarithmically spaced values between a and b.
linspace(a,b,n)

logspace (a,b,n)
21

S O M E U S E F U L A R R AY F U N C T I O N S ( I I )
Command Description
max(A) Returns the algebraically largest element in A if A is a vector. Returns a row vector
containing the largest elements in each column if A is a matrix. If any of the elements
are complex, max(A) returns the elements that have the largest magnitudes.
Similar to max(A) but stores the maximum values in the row vector x and their indices
in the row vector k.
[x,k]=max(A)
Same as max(A) but returns minimum values.
min(A)
Same as [x,k] = max(A) but returns minimum values.
[x,k]=min(A)
Returns a row vector [m,n] containing the sizes of the mxn array A.
size(A)
Sorts each column of the array A in ascending order and returns an array the same size
as A.
sort(A)
Sums the elements in each column of the array A and returns a row vector containing
the sums.
sum(A)
22

E L E M E N T- B Y- E L E M E N T O P E R AT I O N S

Command Description Form Example


+ Scalar-array addition A+b [6,3]+2=[8,5]
- Scalar-array subtraction A-b [8,3]-5=[3,-2]
+ Array addition A+B [6,5]+[4,8]=[10,13]
- Array subtraction A-B [6,5]-[4,8]=[2,-3]
.* Array multiplication A.*B [3,5].*[4,8]=[12,40]
./ Array right division A./B [2,5]./[4,8]=[2/4,5/8]
.\ Array left division A.\B [2,5].\[4,8]=[2\4,5\8]
.^ Array exponentiation A.^B [3,5].^2=[3^2,5^2]
2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
23

M AT R I X O P E R AT I O N S ( I )

Multiplication of Vectors

u=[u1 u2 u3], v=[v1 v2 v3]

u.v=u1v1 +u2v2 +u3v3


Example:
24

M AT R I X O P E R AT I O N S ( I I )

Vector-matrix multiplication

Example:
25

M AT R I X O P E R AT I O N S ( I I I )

Matrix-matrix multiplication Example:


26

M AT R I X O P E R AT I O N S ( I V )

Special matrices
Null matrix 0: 0A=A0=0
Identity (Unity) matrix I: IA=AI=A

Command Description
eye(n) Creates an nxn identity matrix
eye(size(A)) Creates an identity matrix the same size as the matrix A
ones(n) Creates an nxn matrix of ones
ones(m,n) Creates an mxn array of ones
ones(size(A)) Creates an array of ones the same size as the array A
zeros(n) Creates an nxn matrix of zeros
zeros(m,n) Creates an mxn array of zeros
zeros(size(A)) Creates an array of zeros the same size as the array A
27

E X A M P L E O F M AT R I X O P E R AT I O N S

Plot the function


P O LY N O M I A L O P E R AT I O N S U S I N G
28

A R R AY S ( I )
 
where the function of , degree(order): : polynomial’s coefficients

→ using row vector to describe a polynomial


→ can be described as follows:
Ex: [4,-8,7,-5] represents for:
P O LY N O M I A L O P E R AT I O N S U S I N G
29

A R R AY S ( I I )
 To find polynomial roots → roots (a)
 (a): array containing the polynomial coefficients.
 To compute the coefficients of the polynomial whose roots are specified by the array (a)

→ poly(a)
P O LY N O M I A L O P E R AT I O N S U S I N G
30

A R R AY S ( I I I )
Given
  2 polynomials:
P O LY N O M I A L O P E R AT I O N S U S I N G
31

A R R AY S ( V )
 Plotting Polynomials
 Polyval(a,x): evaluates a polynomial at specified values of its independence variable which
can be a matrix or a vector
- E.g. Find the value of at
32

P O LY N O M I A L O P E R AT I O N S
U S I N G A R R AY S ( V I )

Examples
Plot the polynomial f(x) for 0x10
33

PRACTICE EXERCISES
34

EXERCISE 1
 
Type this matrix in Matlab and use Matlab to answer the following questions:

1. Create a vector consisting of the elements in the second column of .


2. Create a vector consisting of the elements in the second row of .
3. Create a 4x3 array consisting of all elements in the second through fourth columns of .
4. Create a 4x3 array consisting of all elements in the second through fourth rows of .
5. Create a 2x3 array consisting of all elements in the first two rows and the last three columns of .
35

EXERCISE 2
Given
  the matrix

1. Find the minimum and maximum values in each column, row.


2. Sort each column and store the result in an array .
3. Sort each row and store the result in an array .
4. Add each column and store the result in an array .
5. Add each row and store the result in an array .
36

EXERCISE 3

 Given the matrices

1. Find
2. Find
3. Find
4. Find
5. Find raised to the third power element-by-element
37

EXERCISE 4
Use Matlab to confirm that:

Plot the polynomial:

over the range -7x1


38

EXERCISE 4
Given
  the matrix

a. Find the maximum and minimum values in each column


b. Find the maximum and minimum values in each row
39

EXERCISE 5
 
Given the matrix

a. Sort each column and store the result in an array B


b. Sort each row and store the result in an array C
c. Add each column and store the result in an array D
d. Add each row and store the result in an array E
40

EXERCISE 6

Create
a.   a three-dimensional array whose three “layers” are these matrices:

b. Use MATLAB to find the largest element in each layer of D and the largest element in D
41

EXERCISE 7

Given
  the matrices

Use MATLAB to
a. Find
b. Find
c. Verify the associative law
d. Verify the communicative law
42

EXERCISE 8

Given
  the matrices

Use MATLAB to
a. Verify the associative property

b. Verify the distributive property

You might also like