Lecture 2 New
Lecture 2 New
M AT R I C E S
1
CONTENTS
Arrays
Two-dimensional Arrays
Element-by-element Operations
Matrix Operations
Polynomial Operations Using Arrays
Practical Exercises
2
V E C T O R S A N D M AT R I C E S
Vectors are Matrices are used to store sets of values of the same type
Matrices:
Visualized as a table of values
Dimensions of a matrix are , where is the number of rows and is the number of columns
All the values are called elements
Vectors:
Be either a row vector or a column vector
Row vector has the dimensions
Column vector has the dimensions
Be equivalent to one-dimensional array
A scalar (one value): dimensions (special case)
3
A R R AY S O F N U M B E R S
Arrays of number can represent data:
Year 1984 1986 1988 1990 1992 1994 1996
Population 127 130 136 145 158 178 211
Arrays of number can represent a vector, e.g., position vector- location of point in a three
dimensional space can be represented by 3 Cartesian coordinates 2,4, and 5.
A position vector that points to the location of point A relative to
point O (the origin of the coordinate system) is defined by
4
ENTERING VECTORS (I)
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:
5
ENTERING VECTORS (II)
brackets.
NOTE: Matlab is not “picky” about how the data is typed in. Spaces can be typed before and/or after
6
7
9
T W O D I M E N S I O N A L A R R AY S : M AT R I C E S
Notes:
() matrix has rows and columns
() is called the size of the matrix
10
C R E AT I N G A M AT R I X
A matrix can be created by typing the elements (numbers) row by row inside square brackets [].
For the transpose of a matrix, enter a prime mark at the end of the array
11
A R R AY A D D R E S S I N G ( V E C T O R S ) ( I )
12
A R R AY A D D R E S S I N G ( V E C T O R S ) ( I I )
13
A R R AY A D D R E S S I N G ( M AT R I C E S ) ( I )
14
A R R AY A D D R E S S I N G ( M AT R I C E S ) (II)
15
A R R AY A D D R E S S I N G ( M AT R I C E S ) ( I 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 V )
17
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.
[u,v,w]=find(A) _Computes the arrays u and v, containing the row and column indices of the nonzero
elements of the matrix A, and the array w, containing the values of the nonzero elements.
The array w may be omitted.
length (A) _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.
linspace(a,b,n) _Creates a row vector of n regularly spaced values between a and b.
logspace (a,b,n) _Creates a row vector of n logarithmically spaced values between a and b.
18
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.
[x,k]=max(A) _Similar to max(A) but stores the maximum values in the row vector x and their indices in the
row vector k.
min(A) _Same as max(A) but returns minimum values.
[x,k]=min(A) _Same as [x,k] = max(A) but returns minimum values.
size(A) _Returns a row vector [m,n] containing the sizes of the mxn array A.
sort(A) _Sorts each column of the array A in ascending order and returns an array the same size as A.
sum(A) _Sums the elements in each column of the array A and returns a row vector containing the sums.
19
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
20
M AT R I X O P E R AT I O N S ( I )
Multiplication of Vectors
21
M AT R I X O P E R AT I O N S ( I I )
22
M AT R I X O P E R AT I O N S ( I I I )
u11 u12
v11 v12
u u21 u22 v
v21 v22
u31 u32
u11 v11 u12v21 u11 v12 u12v22
u.v u21v11 u22v21 u21v12 u22v22
u31v11 u32v21 u31v12 u32v22
23
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
24
E X A M P L E O F M AT R I X O P E R AT I O N S
10 0 x 2
f x 0 2 x 5
3 5 x 7
25
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 ( I )
f x a1 x n a2 x n 1 a3 x n 2 ... an 1 x 2 an x an 1
where the function of , degree(order): : polynomial’s coefficients
→ using row vector to describe a polynomial
→ can be described as follows:
26
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 ( 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)
27
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 ( I I I )
Given 2 polynomials:
28
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 )
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
29
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 )
Example. Plot the polynomial for
30
PRACTICE EXERCISES
31
EXERCISE 1
Type this matrix in Matlab and use Matlab to answer the following questions:
3 7 4 12
5 9 10 2
A
6 13 8 11
15 5 4 1
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 .
32
EXERCISE 2
Given the matrix
3 7 4 12
5 9 10 2
A
6 13 8 11
15 5 4 1
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 .
33
EXERCISE 3
7 16 6 5 3 9
A B C
4 9 12 2 6 8
1. Find
2. Find
3. Find
4. Find
5. Find raised to the third power element-by-element
34
EXERCISE 4
Use Matlab to confirm that:
20 x 3
7 x 2 5 x 10 4 x 2 12 x 3
80 x 5 212 x 4 124 x 3 121x 2 105 x 30
35
EXERCISE 5
Given the matrix
36
EXERCISE 6
Given the matrix
37
EXERCISE 7
b. Use MATLAB to find the largest element in each layer of D and the largest element in D
38
EXERCISE 8
Use MATLAB to
a. Find
b. Find
c. Verify the associative law
d. Verify the communicative law
39
EXERCISE 9
Use MATLAB to
a. Verify the associative property
40