Upload
charmaine-chu
View
216
Download
0
Embed Size (px)
Citation preview
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
1/32
Matrices
Week8
ByJi Hui
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
2/32
Matrices
Matlab standsforMatricesLaboratory
Specifiedto
work
with
data
in
the
form
of
matrix
MeaningofMatrix
Anarrangement
of
data
in
rows
and
columns,
e.g.
atable.
Amathematicalobject,particularmathematical
operationsare
defined
in
linear
algebra:
e.g.
matrixmultiplication
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
3/32
Exampleof
Matrices
Matrices
Dimension:3x2. Dimension:2x4
Vectors
Specialcaseofmatrixwith
ony onerow(oronlyonecolumn
2 1
A= 0 5
4 8
0 5 3 1B =
2 0 9 6
1
2c =
3
4
d = 6 3 2
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
4/32
Exampleof
matrix
data
A readymix concrete company has three factories (S1, S2
and S3) which must supply three building sites (D1, D2and D3). The costs, in some suitable currency, of
transporting a load of concrete from any factory to any
site are given by the following cost table:
D1 D2 D3
S1 3 12 10
S2 17 18 15
S3 7 10 24
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
5/32
Reviewson
matrix
and
its
operations
Index,elementsandmatrix
,
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
6/32
Matrixalgebra
Transpose
,
,
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
7/32
(cont)
Matrixaddition
Scalemultiplication
, , ,
, ,
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
8/32
(cont)
Matrixmultiplication
, , ,
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
9/32
(cont)
Specialmatrix
Nodivisionformatrices
IfAisinvertiblesquarematrix,thereexista
matrix
,s.t.
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
10/32
Creatingmatrix
Creatingvector
usecomma
(or
empty
space)
to
separate
items
e.g.[1,8,4,0]or[1840]
Creatingmatrix
useasemicolon
to
indicate
the
end
of
arow
e.g.>>a=[1,2,3;4,5,6]
a=
1 2 3
4 5 6
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
11/32
Constructingbigmatrixfromsmallermatrices
e.g.>>
c =
[1
2;
34];
>>x=[56];
>>a=[c;x]
b=
1 2 5
3
4
6
(cont)
1 2
3
4
5 6a =
c
x=
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
12/32
Subscripts
Individualelementsofamatrixarereferenced
withtwo
subscripts,
the
first
for
the
row,
and
thesecondforthecolumn,
e.g.a(2,3)referselementsat2throwand3th
column>>a(2,3)
ans =
6
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
13/32
(cont)
Alternatively,youmayuseasingleindexby
thinkamatrix
as
being
unwounded
column
bycolumn
e.g. >>a(3)
ans
=2
1
2
3
4 5 6
1
4
25
3
6
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
14/32
Thecolon
operator
for
subscripts
Thecolonoperatorisextremelypowerful,and
providesfor
very
efficient
ways
of
handling
matrices
e.g.ifaisthematrix
(1) >>a(2:3,1:2)
ans =
45
78
1 2 3
4 5
67 8 9
%returnssecondandthirdrows,firstandsecond
columns
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
15/32
(cont)(2)>>a(2,:)
ans =7 8 9
%returnsthewholesecondrow
(3)>>a(1:2,2:3)=[0,0;0,0]
ans =1 0 0
4 0 0
7
8
9% replacesthe2by2submatrix composedofthefirstandsecondrowand thesecondandthirdcolumnwithasquare matrixof0s).
1 2 3
4 5
67 8 9
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
16/32
Whatishappeningusingcolon
operator
thecolonoperatorisbeingusedtocreatevector
subscripts.
However,acolonbyitselfinplaceofasubscriptdenotesalltheelementsofthecorrespondingroworcolumn.
Youcan
use
vector
subscripts
to
get
more
complicatedeffects, e.g.a(:,[13])=b(:,[42])replacesthefirstandthird
columns
of
a
by
the
fourth
and
second
columns
of
b
(a
andbmusthavethesamenumberofrows).
Whathappentoa(:)
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
17/32
Thekeyword
endinsubscripts.
Thekeywordend referstothelastrowor
columnof
an
array.
e.g.>>a[2,2:end]
ans =
5 6
1 2 3
4 5 6
7
8
9
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
18/32
Elementarymatrices
Thereisagroupoffunctionstogenerate
elementarymatrices,
See
help
elmat
thefunctionszeros(n),ones(n) andrand(n) generatenbynmatricesof1s,0sandrandomnumbers,respectively.
>>zeros(3)
ans =
0 0 0
0
0
0
0 0 0
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
19/32
(cont)
functioneye(n) generatesannn identitymatrix
>>eye(3)
ans =
1 0 0
0 1 0
0 0 1
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
20/32
Anexampletoconstructtridiagonal
matrix
>>a=2*eye(5);
>>a(1:4,
2:5)
=a(1:4,
2:5)
+eye(4);
>>a(2:5,1:4)=a(2:5,1:4)+eye(4)
a=
21000
12100
01210
00121
00012
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
21/32
transpose Thetransposeoperator(apostrophe)turnsrows
into
columns
and
vice
versa. e.g.>>a=[1,2,3;4,5,6]
a=
1 2 3
4
5
6>>a'
ans =
1
42 5
3 6
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
22/32
Changingshape
of
matrices
Thefunction reshape(x,m,n)returnsthembyn
matrix
whose
elements
are
taken
column
wise
from
x e.g.>>a=[123;456]
a=
1 2 3
4
5
6
>>reshape(a,3,2)
ans =
1
54 3
2 6
1 2
3
4 5 6
1
42
5
36
1
54 3
2 6
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
23/32
Manipulatingmatrices
Herearesomefunctionsformanipulatingmatrices.Seehelpfordetails.
Diag extractsorcreatesadiagonal.
Fliplr
flipsfromlefttoright.
Flipud flipsfromtoptobottom.
rot90
rotates.
tril
extractsthelowertriangularpart
triu
extractstheuppertriangularpart.
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
24/32
Someusefulfunctionsformatrix
information
size
Givethe
dimension
information
of
amatrix
Length
Givethenumberofelementsofavector
find
Findtheindexofnonzeroelementsofmatrix.
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
25/32
Multidimensionalarrays
MATLABarrayscanhavemorethantwodimensions.
e.g.>>a(:,:,1)=[1:2;3:4]
>>a(:,:,2)=[5:6;7:8]
a(:,:,1)=
1 2
3
4a(:,:,2)=
5 6
7 8
Ithelps
to
think
of
the
3D
array
aas
aseries
of
pages,
withamatrixoneachpage.Thethirddimensionofanumbersthepages
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
26/32
Matlab functionson
matrices
Theoperationsoneachelementsofthematrix
couldbe
different
on
the
operations
on
thematricesasamathematicalobject
Somebuiltinmatlab functionoperatesonevery
elementof
the
matrix,
as
you
would
expect
e.g.>>sin([1,2;3,4])
ans
=0.8415 0.9093
0.1411 0.7568
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
27/32
(cont)
However,somebuiltinmatlab functionoperates
thematrix
column
wise
e.g. >>sum([1,2;3,4])
ans=
4
6
Ifyouarenotsurewhetheraparticularfunction
operatescolumnwiseorelementbyelementon
matrices,youcanalwaysrequesthelp.
1 2
3
4
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
28/32
Basicmatrix
operators
Matrixaddition(elementwise)>>a=[1,2;3;4];
>>b=[5,6;0,1];
>>a+b
ans=
6 8
3
3
Matrixsubtraction(elementwise)
>>ab
ans =
4
4
3 5
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
29/32
(cont) Matrixmultiplication(NOTelementwise)
In
general
>>a*b
ans =
5
415 14
>>b*a
ans =23 34
3 4
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
30/32
Howto
do
element
wise
multiplication
Elementwisemultiplicationvs matrix
multiplication
>>a.*
b
ans =
5
120 4
, , , , , ,
1
( vs ) ( * )*n
i j i j i j i j i k k j
k
A A B a b B a b
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
31/32
Matrixexponential
matrixexponential(NOTelementwise)
Ashouldbeasquarematrix
>>a^3
Ifneedelementwiseexponential
>>a.^3
8/8/2019 CZ1102 Computing & Problem Solving Lecture 6
32/32
Someother
useful
matrix
function
det
determinant.
eig
eigenvaluedecomposition.
inv
Inversematrix
svd
singularvaluedecomposition.