0% found this document useful (0 votes)
11 views40 pages

Lecture 2 New

The document discusses vectors, matrices, and their operations in MATLAB. It defines vectors and matrices, and how they can be represented and entered in MATLAB. It describes how to perform element-by-element operations and various matrix operations, such as vector-matrix multiplication and matrix-matrix multiplication. It also discusses array addressing and useful array functions in MATLAB.

Uploaded by

Kiệt Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views40 pages

Lecture 2 New

The document discusses vectors, matrices, and their operations in MATLAB. It defines vectors and matrices, and how they can be represented and entered in MATLAB. It describes how to perform element-by-element operations and various matrix operations, such as vector-matrix multiplication and matrix-matrix multiplication. It also discusses array addressing and useful array functions in MATLAB.

Uploaded by

Kiệt Hoàng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

LECTURE 2: VECTORS AND

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:

[1984 1986 1988 1990 1992 1994 1996]


1984
[127 130 136  158
145 127 178 211]
   
1986 130 
or in column: 1988 136 
   
1990  145 
1992 158 
   
1994 178 
1996  211
   

5
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 “=“.


 A space can be added in addition to the comma, or type more than one space.

6
7

ENTERING VECTORS (IV)


To create a COLUMN VECTOR:
1) Type a left bracket [,
2) Enter the elements with a semicolon between
them, or press <ENTER> after each element.
3) 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)
ENTERING VECTORS (VII)
logspace function creates a logarithmically
spaced vector
Syntax:
 logspace(x,y,n) creates a vector with n values
in the inclusive range from to
- If is omitted, the default is 50 points

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

A square matrix has the number of rows and columns equaled

The number of rows and columns can be different

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

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]

20
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:

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

Vector-matrix multiplication Example:

u11 u12 u13   v11 


u  u21 u22 u23  v  v21 
u31 u32 u33  v31 
 u11 v11  u12v21  u13v31 

u.v  u21v11  u22v21  u23v31  
u31v11  u32v21  u33v31 

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

Matrix-Matrix Multiplication Example:

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

Plot the function

 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:

Example: [4,-8,7,-5] represents for:

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

Given the matrices

 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

Plot the polynomial:


y  x 3  13 x 2  52 x  6

over the range -7x1

35
EXERCISE 5
Given the matrix

a. Find the maximum and minimum values in each column


b. Find the maximum and minimum values in each row

36
EXERCISE 6
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

37
EXERCISE 7

a. Create 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

38
EXERCISE 8

Given the matrices

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

39
EXERCISE 9

Given the matrices

Use MATLAB to
a. Verify the associative property

b. Verify the distributive property

40

You might also like