What Is a Matrix - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

What Is a Matrix

Description:

temp = A B cos(mp/6) C sin(mp/6) For ParisHi, we want. 55 = A B cos(1p/6) C sin(1p/6) 55 = A B cos(2p/6) C sin(2p/6) ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 41
Provided by: Snoe9
Category:
Tags: matrix | sin

less

Transcript and Presenter's Notes

Title: What Is a Matrix


1
What Is a Matrix?
  • COMP 116
  • August 29, 2007

2
Outline What is a matrix?
  • A support for something growing
  • A table of numbers
  • A stack (column) of row vectors
  • Creating, Indexing, Operating
  • A row of column vectors
  • A collection of data in columns
  • Plotting
  • A system of equations or polynomials
  • A transformation for a vector space
  • A linear operator on a vector space
  • A change of variables or coordinates

3
www.whatisthematrix.com
  • An interconnected network or enclosure in which
    something grows

4
A matrix is a table of numbers
  • Create
  • With functions
  • rand(3,4)ones(3,4)zeros(3)
  • eye(4)
  • diag(1,2,3,4)
  • 1./hilb(4)
  • Helpwin gallery
  • Search for matrices

5
A table of numbers
  • Create
  • With brackets semicolon1 2 3 467, 8, 9
    10 11 12
  • Commas optional
  • Return in place of semicolon
  • Colon or functions to generate rows

6
Indexing a table of numbers
  • With row, column scalars
  • m(3,2) 3rd row, 2nd column
  • With vectors
  • m(3, 24) 3rd row, cols 24
  • m(23,23) 2x2 submatrix
  • Colon abbreviation
  • m(3,) all of row 3
  • m(, 2) all of column 2
  • Colon/end
  • m(2end-1,2end-1) internal

7
An example Build a 5x5
  • Make 5x5 matrix with 1/2 on diagonal and 1/4
    just above
  • with brackets
  • m 1/2 1/4 0 0 0 0 1/2 1/4 0 0 0 0 1/2 1/4 0
    0 0 0 1/2 1/4
  • Zeros indexing
  • m zeros(5)
  • m(1,1) 1/2 m(2,2) 1/2 m(3,3) 1/2 m(4,4)
    1/2 m(5,5) 1/2
  • m(1,2) 1/4 m(2,3) 1/4 m(3,4) 1/4 m(4,5)
    1/4
  • m
  • Zeros loops
  • ma zeros(5)
  • for i 15, ma(i,i) 1/2 end
  • for i 14, ma(i,i1) 1/4 end
  • ma

0.50 0.25 0 0 0
0 0.50 0.25 0 0 0
0 0.50 0.25 0 0
0 0 0.50 0.25 0 0
0 0 0.50
8
An Example Build a 5x5
  • More esoteric I wanted to see how little
    typing I could do.
  • Adding identity matrices
  • m zeros(5)
  • m(14,25) eye(4)/4
  • m m eye(5)/2
  • Same thing only shorter
  • m eye(5)/2
  • m(14,25) m(14,25) eye(4)/4
  • Shortest combine eye and diag. Easiest to
    read, too.
  • m eye(5)/2 diag(ones(1,4)/4, 1)

0.50 0.25 0 0 0
0 0.50 0.25 0 0 0
0 0.50 0.25 0 0
0 0 0.50 0.25 0 0
0 0 0.50
9
A table of numbers operations
  • Size of a matrix
  • m rand(3,4)
  • size(m) rows, columns
  • size(m,1) rows
  • size(m,2) columns
  • r,c size(m) get both
  • m' Transpose
  • size(m')

10
Arithmetic Logic Operations
  • A B operate on same size
  • Arithmetic Logic , -, , lt, gt, ,
  • Array operators ., ./, .
  • Dimensions must agree!
  • scalar matrix promote scalar to matrix
  • 5A, 5A, 5.A, (5A 5.A)

11
A matrix is a stack of row vectors
  • Example average hi/lo temperatures for twelve
    months in two cities
  • parisHi55 55 59 64 68 75 81 81 77 70 63 55
  • parisLo39 41 45 46 55 61 64 64 61 54 49 41
  • rioHi 84 85 83 80 77 76 75 76 75 77 79 82
  • rioLo 73 73 72 69 66 64 63 64 65 66 68 71
  • temp parisHi parisLo rioHi rioLo
  • size(temp)
  • max(temp) Max in each month

12
A matrix is a row of column vectors
  • temp' Transpose rows become columns
  • tt parisHi' parisLo' rioHi' rioLo' Same
  • max(temp) max per month (12 cols)
  • max(tt) max per city (4 cols)
  • max(temp') same
  • Indexing columns with colon
  • tt(9,) September temperatures
  • tt(parisHigtrioHi,) rows w/ paris warmer

13
A collection of data in columns
  • plot(temp') temperature plots
  • legend(parisHi, parisLo, rioHi, rioLo)
  • Many other functions operate on columns
  • sum, prod, mean, std, diff, cumsum,
  • Aside row variants
  • sum(temp, 2) sum rows to give 4x1 vector
  • sum(temp')' same

14
A matrix is a system of equations
  • A matrix is a compact way of writing related
    equations
  • Buy a apples, b bananas, and c carrots, when the
    prices at three stores are

15
A system of equations
  • A matrix is a compact way of writing related
    equations
  • Buy a apples, b bananas, and c carrots, when the
    prices at three stores are

food L 1.29a 0.84b 0.42c harrisT
1.24a 0.86b 0.41c grocery 1.09a
0.98b 0.45c
16
A system of equations
  • A matrix is a compact way of writing related
    equations
  • Buy a apples, b bananas, and c carrots, when the
    prices at three stores are
  • Prices quantity total cost _at_ store

17
Matrix multiplication
  • C AB
  • Inner dimensions must agree jxk kxm gives jxm
    matrix
  • C(j,m) A(j, ) B(, m)
  • C(j,m) sum(A(j,) . B(,m)') same

18
Matrix multiplication
  • MATLAB C AB
  • C program
  • double AMaNa, BMbNb, CMcNc
  • / Check Na Mb, Mc Ma, Nc Nb
  • for (j 0 j lt Ma j) for (m 0 m lt Nb
    m) Cjm 0 for (k 0 k lt
    Na k)
  • Cjm Cjm AjkBkm

19
Matrix multiplication
  • item prices item quantities store totals
  • Matrix vector vector
  • Matrix matrix matrix
  • A2 AA, so A must be square

20
Solving a system of equations
  • Square matrices same number of equations as
    unknowns (variables)
  • Generally a unique solution x to Axb, when A and
    b are known
  • x A\b Matrix left division
  • Continuing the food example Are there quantities
    abc that will cost 101011?

21
Solving a system of equations
  • x A\b satisfies Axb, so buy ¾ lb
    apples, 6.3 lb bananas, 8 7/8 lb carrots,to
    spend 10 at foodL or harrisT and 11 at the
    corner grocery

gtgt x prices \ 101011 x 0.7512
6.3146 8.8732 gtgt prices x ans 10
10 11
22
Static equilibrium (balanced forces)
  • Triangular bridge ABC
  • A fixed
  • 10N load on B
  • C rolls
  • Forces f1, f2, f3 on bars ( expand - contract)
  • horizontal and vertical forces must balance at
    vertices (or they'd move collapse bridge)
  • Let's see if we can find three equations for
    these three variables.

g
q
23
Static equilibrium
  • A is fixed, so we need no equation.(I.e., the
    ground cancels forces)
  • B may move 2 eqns
  • Bh f1cos(q) - f2cos(g) 0
  • Bv f1sin(q) f2sin(g) - Load 0
  • C may move horizontally 1 eqn
  • Ch f3 f2cos(g) 0
  • Matix form Mfb

24
Static equilibrium in MATLAB
  • Build the matrix and solve
  • M cos(theta) -cos(gamma) 0
    sin(theta) sin(gamma) 0 0
    cos(gamma) 1
  • b 0 Load 0
  • forces M \ b
  • forces 12 push up to balance Load, thus
    push apart, countered by contraction of 3.

25
Non-square systems of equations
  • Rectangular matrices
  • Underdetermined (fewer eqns than vars)
  • Overdetermined (more eqns than vars)
  • MATLAB solves Axb "in least squared sense"
  • Finds x that minimizes the squared
    residual (A x b)2

26
An example
  • Data friction in response to a force
  • Looks like a line
  • Best line ymx c?
  • Find m,c.
  • Evaluate error.

27
Fit a line
  • We have x and y
  • We'd like m, c that
  • 5.84 m1c
  • 8.87 m2c
  • 11.76 m3c
  • 13.71 m4c
  • 14.52 m5c

28
Fit a line
  • We'd like Amc friction
  • EasyA\frictiongives mc



29
Fit a line in MATLAB
  • A(,1) (110)'
  • A(,2) ones(10,1)
  • b 5.84 8.87 11.76 13.71 14.52 17.71 21.80
    24.25 25.23 29.18'
  • A\b
  • 2.5121
  • 3.4707
  • plot(110,b,'r.', 110,ans(1)(110)ans(2),'b-')

30
Error in fitting a line
  • mc A\b
  • Amc will be close to b,but not exact.
  • Compute residual
  • resid Amc -b
  • error resid' resid
  • error 5.8308, which is min. over all lines
  • We use squared residual to measure error

31
Least squares reprise
  • Least squares requires
  • a mathematical model giving a function to fit,
  • parameters of the model that must be found,
  • observed data used to calculate best parameters
  • Linear least squares problems are those that can
    be written as Axb, where
  • matrix A and column vector b are known, and
  • x is the column vector of unknowns.
  • Error (scalar) is squared residual
  • error (Ax-b)'(Ax-b)

32
Fitting other models Paris/Rio
  • Example average hi/lo temperatures for twelve
    months in two cities
  • parisHi55 55 59 64 68 75 81 81 77 70 63 55
  • parisLo39 41 45 46 55 61 64 64 61 54 49 41
  • rioHi 84 85 83 80 77 76 75 76 75 77 79 82
  • rioLo 73 73 72 69 66 64 63 64 65 66 68 71
  • temp parisHi parisLo rioHi rioLo

33
Model the annual temperature
  • plot(temp')
  • Annual cycle sine wave?
  • Fit best sine curve to data

34
Fitting a sine/cosine curve
  • for months m112 (data)
  • Consider variables A, B, C
  • Fit a curve of the formtemp A B cos(mp/6)
    C sin(mp/6)
  • For ParisHi, we want
  • 55 A B cos(1p/6) C sin(1p/6)
  • 55 A B cos(2p/6) C sin(2p/6)
  • 59 A B cos(3p/6) C sin(3p/6)
  • 64 A B cos(4p/6) C sin(4p/6)

Write asMABCtemp?
35
Fitting a sine cosine
Paris Rio hi/lo hi/lo
  • We'd like MABC temp'



36
Fitting a sine cosine in MATLAB
  • We'd like MABC temp'
  • m 112 months
  • CON ones(12,1) columns of M
  • COS cos(2pi/12 m)'
  • SIN sin(2pi/12 m)'
  • fit CON COS SIN \ temp' best fit
  • clf plot(temp', '')
    plot data
  • hold on plot(CON COS SINfit) plot fit

37
Error in fitting sine cosine
  • We'd like MABC temp'
  • We have fit
  • resid Mfit-temp'
  • error sum(resid .resid)
  • Gives four errors, one for each city

38
A space of faces
  • In assn1, you turned images into numbers and
    averaged them...
  • The set of all faces you can make by linear
    combinations is a vector spaceA point (.5, -.2,
    .7)
  • .5 x -.2 y .7 z
  • A matrix is a set of rows or columns that define
    a vector space.

39
Space of faces
  • The following is just for fun
  • The idea comes from Turk, M. and Pentland, A.
    (1991). Eigenfaces for recognition. Journal of
    Cognitive Neuroscience, 3(1)71--86.http//www.cs
    .ucsb.edu/mturk/research.htm

40
Average student
  • 67 images, each a 221 x 201 matrix,stored as a
    67x44421 matrix
  • Align eyes, nose, mouth, ears
  • Principal components analysis (PCA) finds
    greatest deviation from average
  • Eigenvectors added or subtracted from average
    face
Write a Comment
User Comments (0)
About PowerShow.com