Professional Documents
Culture Documents
Lecture 2
Lecture 2
Lecture 2
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
A R R AY S O F N U M B E R S
5
In Matlab, a vector, or any list of numbers, can be entered in a horizontal (row) or vertical
(column) vectors.
Example:
brackets.
NOTE: Matlab is not “picky” about how the data is typed in. Spaces can be typed before and/or
space.
7
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
M AT R I X O P E R AT I O N S ( I )
Multiplication of Vectors
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 )
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
A R R AY S ( I )
where the function of , degree(order): : polynomial’s coefficients
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 0x10
33
PRACTICE EXERCISES
34
EXERCISE 1
Type this matrix in Matlab and use Matlab to answer the following questions:
EXERCISE 2
Given
the matrix
EXERCISE 3
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:
EXERCISE 4
Given
the matrix
EXERCISE 5
Given the matrix
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