CH 03
CH 03
2
3.0
3
3.1 ADDITION AND SUBTRACTION
4
3.1 ADDITION AND SUBTRACTION
6
3.1 ADDITION AND SUBTRACTION
EXAMPLE
𝐴𝐴11 𝐴𝐴12 𝐴𝐴13 𝐵𝐵11 𝐵𝐵12 𝐵𝐵13
For 𝐴𝐴 = and 𝐵𝐵 =
𝐴𝐴21 𝐴𝐴22 𝐴𝐴23 𝐵𝐵21 𝐵𝐵22 𝐵𝐵23
7
3.1 ADDITION AND SUBTRACTION
8
3.1 ADDITION AND SUBTRACTION
EXAMPLE
𝐴𝐴11 𝐴𝐴12 𝐴𝐴13
For c a scalar and 𝐴𝐴 =
𝐴𝐴21 𝐴𝐴22 𝐴𝐴23
9
3.2 ARRAY MULTIPLICATION
11
3.2 ARRAY MULTIPLICATION
Example:
>> A = [ 1 4 3; 2 6 1; 5 2 8 ]
A = 1 4 3
2 6 1
5 2 8
>> B = [ 5 4; 1 3; 2 6 ]
B = 5 4
1 3
2 6
>> A * B
ans = 15 34
18 32
43 74
12
3.2 ARRAY MULTIPLICATION
Example:
Note that B*A is not even defined,
because the number of columns in B is
not equal to the number of rows in A.
Attempting to compute B*A produces
an error:
>> B * A
Error using *
Inner matrix dimensions must agree.
13
3.2 ARRAY MULTIPLICATION
14
3.2 ARRAY MULTIPLICATION
15
3.2 ARRAY MULTIPLICATION
>> h = [ 2 4 6 ] >> h * v
h = ans =
2 4 6 4
>> v = [ -1 0 1 ]' >> v * h
v = ans =
-1 -2 -4 -6
0 0 0 0
1 2 4 6
17
3.2 ARRAY MULTIPLICATION
• Any combination of
0
1
vertical or horizontal >> dot(h,v)
ans =
vectors 4
>> dot(v,h)
• Result is always a scalar ans = 4
18
3.2 ARRAY MULTIPLICATION
19
3.3 ARRAY DIVISION
Identity matrix
• Square matrix with ones on main diagonal
and zeros elsewhere
Main diagonal goes from top left to bottom
right
• When do matrix multiplication on any array
or vector with the identity matrix, array or
vector is unchanged
True whether multiply with identity matrix on
left or on right
• MATLAB command eye(n) makes an n×n
identity matrix
20
3.3 ARRAY DIVISION
Inverse of a matrix:
21
3.3 ARRAY DIVISION
22
3.3 ARRAY DIVISION
Determinants:
A determinant is a function
associated with square matrices
• In math, determinant of A is written as
det(A) or |A|
• In MATLAB, compute determinant of A
with det(A)
• A matrix has an inverse only if it is
square and its determinant is not zero
If you don't remember much about determinants, go
back to your linear algebra book and review them
23
3.3 ARRAY DIVISION
Left division, \:
Right division, /:
Right division is the other kind of
MATLAB's array division
• Used to solve the matrix equation
XC=D
C is a square matrix, X, D are row vectors
Solution is X = D·C-1
In MATLAB, solve by using right
division operator (/), i.e.,
>> X = D / C
26
3.4 ELEMENT-BY-ELEMENT OPERATIONS
27
3.4 ELEMENT-BY-ELEMENT OPERATIONS
Do elementwise multiplication,
division, exponentiation by putting a
period in front of the arithmetic
operator
28
3.4 ELEMENT-BY-ELEMENT OPERATIONS
29
3.4 ELEMENT-BY-ELEMENT OPERATIONS
30
3.4 ELEMENT-BY-ELEMENT OPERATIONS
ELEMENTWISE MULTIPLICATION
• Use .* to get elementwise multiplication
(notice period before asterisk)
• Both matrices must have the same
dimensions
>> A = [1 2; 3 4];
>> B = [0 1/2; 1 -1/2];
>> C = A .* B
>> C =
0 1
3 -2
31
3.4 ELEMENT-BY-ELEMENT OPERATIONS
33
3.4 ELEMENT-BY-ELEMENT OPERATIONS
EXAMPLE
>> A = [1 2; 3 4];
>> B = [0 1/2; 1 -1/2];
>> A .* B
>> ans
0 1
3 -2
>> A * B
ans =
2.0000 -0.5000
4.0000 -0.5000
34
3.4 ELEMENT-BY-ELEMENT OPERATIONS
35
3.5 USING ARRAYS IN MATLAB BUILT-IN FUNCTIONS
36
3.5 USING ARRAYS IN MATLAB BUILT-IN FUNCTIONS
37
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
• median(v) – median
• std(v) – standard deviation
• det(A) – determinant of square matrix A
• dot(v,w) – dot (inner product); v, w
both vectors of same size but any dimension
• cross(v,w) – cross product; v, w must
both have three elements but any
dimension
• inv(A) – inverse of square matrix A
39
3.6 BUILT-IN FUNCTIONS FOR ANALYZING ARRAYS
40
3.7 GENERATION OF RANDOM NUMBERS
41
3.7 GENERATION OF RANDOM NUMBERS
42
3.7 GENERATION OF RANDOM NUMBERS
45
3.7 GENERATION OF RANDOM NUMBERS
46
3.7 GENERATION OF RANDOM NUMBERS
47