what is a matrix? comp 116 august 29, 2007. outline: what is a matrix? a support for something...

40
What Is a Matrix? COMP 116 August 29, 2007

Upload: beverly-quinn

Post on 05-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

What Is a Matrix?

COMP 116August 29, 2007

Page 2: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Outline: What is a matrix?Outline: What is a matrix?

A support for something growingA table of numbers

A stack (column) of row vectorsCreating, Indexing, Operating

A row of column vectorsA collection of data in columns

Plotting

A system of equations or polynomialsA transformation for a vector space

A linear operator on a vector spaceA change of variables or coordinates

Page 3: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

www.whatisthematrix.comwww.whatisthematrix.com

An interconnected network or enclosure in which something grows

Page 4: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A matrix is: a table of numbers

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”

Page 5: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A table of numbersA table of numbers

Create With brackets & semicolon[1 2 3; 4:6;7, 8, 9; 10 11 12]

Commas optionalReturn in place of semicolonColon or functions to generate rows

Page 6: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Indexing a table of numbers

Indexing a table of numbers

With row, column scalars m(3,2) % 3rd row, 2nd column

With vectorsm(3, 2:4) % 3rd row, cols 2:4m(2:3,2:3) % 2x2 submatrix

Colon abbreviationm(3,:) % all of row 3m(:, 2) % all of column 2

Colon/end m(2:end-1,2:end-1) % internal

Page 7: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

An example: Build a 5x5An 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 & indexingm = 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 & loopsma = zeros(5);for i = 1:5, ma(i,i) = 1/2; endfor i = 1:4, ma(i,i+1) = 1/4; endma

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

Page 8: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

An Example: Build a 5x5An Example: Build a 5x5 % More esoteric: I wanted to see how little typing I could do.

%Adding identity matricesm = zeros(5)m(1:4,2:5) = eye(4)/4m = m + eye(5)/2

%Same thing only shorterm = eye(5)/2 m(1:4,2:5) = m(1:4,2:5) + 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

Page 9: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A table of numbers: operations

A table of numbers: operations

Size of a matrix m = rand(3,4);size(m) % [#rows, #columns]

size(m,1) % #rowssize(m,2) % #columns[r,c] = size(m); % get both

m' % Transposesize(m')

Page 10: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Arithmetic & Logic Operations

Arithmetic & Logic Operations

A + B: operate on same sizeArithmetic & Logic: +, -, ==, <, >, &, |Array operators: .*, ./, .^

Dimensions must agree!scalar + matrix: promote scalar to matrix5+A, 5==A, 5.*A, (5*A == 5.*A)

Page 11: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A matrix is: a stack of row vectors

A matrix is: a stack of row vectors

Example: average hi/lo temperatures for twelve months in two cities

parisHi=[55 55 59 64 68 75 81 81 77 70 63 55];parisLo=[39 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

Page 12: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A matrix is: a row of column vectors

A matrix is: a row of column vectors

temp' % Transpose: rows become columnstt = [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 colontt(9,:) % September temperaturestt(parisHi>rioHi,:) % rows w/ paris warmer

Page 13: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A collection of data in columns

A collection of data in columnsplot(temp') % temperature plots

legend(‘parisHi‘, ‘parisLo‘, ‘rioHi‘, ‘rioLo‘)

Many other functions operate on columns:

sum, prod, mean, std, diff, cumsum,…

Aside: row variantssum(temp, 2)

% sum rows to give 4x1 vectorsum(temp')'

% same

Page 14: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A matrix is: a system of equations

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:

apple

banana

carrot

food L 1.29 0.84 0.42

harris T 1.24 0.86 0.41

grocery 1.09 0.98 0.45

Page 15: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A system of equationsA 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.29*a + 0.84*b + 0.42*c

harrisT = 1.24*a + 0.86*b + 0.41*c

grocery = 1.09*a + 0.98*b + 0.45*c

Page 16: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A system of equationsA 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 @ store

⎥⎥⎥

⎢⎢⎢

⎡→

⎥⎥⎥

⎢⎢⎢

⎥⎥⎥

⎢⎢⎢

grocery

harrisT

foodL

c

b

a

*

0.45 0.98 1.09

0.41 0.86 1.24

0.42 0.84 1.29

Page 17: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Matrix multiplicationMatrix multiplication

C = A*BInner dimensions must agree:

jxk * kxm gives jxm matrix

C(j,m) = A(j, :) * B(:, m)C(j,m) = sum(A(j,:) .* B(:,m)') % same

A B C

Page 18: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Matrix multiplicationMatrix multiplication

MATLAB: C = A*BC program

double A[Ma][Na], B[Mb][Nb], C[Mc][Nc];/* Check Na == Mb, Mc == Ma, Nc == Nb

for (j = 0; j < Ma; j++)for (m = 0; m < Nb; m++) C[j][m] = 0;

for (k = 0; k < Na; k++)C[j][m] = C[j][m] + A[j][k]*B[k][m];

Page 19: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Matrix multiplicationMatrix multiplication

item prices * item quantities = store totals

Matrix * vector = vectorMatrix * matrix = matrix

A^2 == A*A, so A must be square

A B C

Page 20: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Solving a system of equations

Solving a system of equations

Square matrices: same number of equations as unknowns (variables)

Generally a unique solution x to Ax=b, when A and b are known:

x = A\b; % Matrix left division

Continuing the food example: Are there quantities [a;b;c] that will cost [10;10;11]?

Page 21: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Solving a system of equations

Solving a system of equations

x = A\b satisfies Ax=b, 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

>> x = prices \ [10;10;11]

x = 0.7512 6.3146 8.8732

>> prices * xans = 10 10 11

Page 22: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Static equilibrium (balanced forces)

Static equilibrium (balanced forces)

Triangular bridge ABCA fixed10N load on BC 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.

A

B

C

1 2

3

Page 23: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Static equilibrium Static equilibrium

A is fixed, so we need no equation.(I.e., the ground cancels forces)

B may move: 2 eqnsBh: f1*cos() - f2*cos() = 0Bv: f1*sin() + f2*sin() - Load = 0

C may move horizontally: 1 eqnCh: f3 + f2*cos() = 0

Matix form: M*f=b

A

B

C

1 2

3

f1

f2

f3* =

0

Load

0

cos( ) -cos( ) 0

sin( ) sin( ) 0

0 cos() 1

Page 24: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Static equilibrium in MATLAB

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 = % 1&2 push up to balance Load, & thus push apart, % countered by contraction of 3.

A

B

C

1 2

3

5.7735

5.7735

-2.887

f1

f2

f3* =

0

Load

0

cos( ) -cos( ) 0

sin( ) sin( ) 0

0 cos() 1

Page 25: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Non-square systems of equations

Non-square systems of equations

Rectangular matricesUnderdetermined (fewer eqns than vars)Overdetermined (more eqns than vars)MATLAB solves Ax=b "in least squared sense"

Finds x that minimizes the squared residual: (A x – b)2

Page 26: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

An exampleAn example

Data: friction in response to a force:

force 1 2 3 4 5 6 7 8 9 10friction 5.84 8.87 11.76 13.71 14.52 17.71 21.8 24.25 25.23 29.18

Looks like a line…Best line y=mx + c? Find m,c.Evaluate error.

Page 27: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Fit a lineFit a line

We have x and y:

force 1 2 3 4 5 6 7 8 9 10friction 5.84 8.87 11.76 13.71 14.52 17.71 21.8 24.25 25.23 29.18

We'd like m, c that5.84 = m*1+c8.87 = m*2+c11.76 = m*3+c13.71 = m*4+c14.52 = m*5+c

xy

Page 28: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Fit a lineFit a line

We'd like A*[m;c] = friction:1 12 13 14 15 16 17 18 19 1

10 1

m

c* =

5.848.87

11.7613.7114.5217.7121.8

24.2525.2329.18

Easy:A\frictiongives [m;c]

Page 29: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Fit a line in MATLABFit a line in MATLAB

A(:,1) = (1:10)';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.4707plot(1:10,b,'r.', 1:10,ans(1)*(1:10)+ans(2),'b-');

Page 30: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Error in fitting a lineError in fitting a line

mc = A\b; %A*mc will be close to b,

but not exact.

%Compute residual:resid = A*mc -b;

error = resid' * resid%error = 5.8308, which is min. over all lines%We use squared residual to measure

error…

Page 31: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Least squares repriseLeast 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 Ax=b, where

matrix A and column vector b are known, and x is the column vector of unknowns.

Error (scalar) is squared residualerror = (Ax-b)'*(Ax-b)

Page 32: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Example: average hi/lo temperatures for twelve months in two cities

parisHi=[55 55 59 64 68 75 81 81 77 70 63 55];parisLo=[39 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];

Fitting other models: Paris/Rio

Fitting other models: Paris/Rio

Page 33: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Model the annual temperature

Model the annual temperature

plot(temp')

Annual cycle: sine wave?

Fit best sine curve to data

Page 34: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Fitting a sine/cosine curveFitting a sine/cosine curve

for months m=1:12 (data)Consider variables A, B, CFit a curve of the form

temp = A + B cos(m/6) + C sin(m/6)

For ParisHi, we want55 = A + B cos(1/6) + C sin(1/6)55 = A + B cos(2/6) + C sin(2/6)59 = A + B cos(3/6) + C sin(3/6)64 = A + B cos(4/6) + C sin(4/6)

Write asM*[A;B;C]=temp?

Page 35: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

=*

Fitting a sine & cosineFitting a sine & cosine

We'd like M*[A;B;C] = temp':1 cos(1* pi/6) sin(1* pi/6)1 cos(2* pi/6) sin(2* pi/6)1 cos(3* pi/6) sin(3* pi/6)1 cos(4* pi/6) sin(4* pi/6)1 cos(5* pi/6) sin(5* pi/6)1 cos(6* pi/6) sin(6* pi/6)1 cos(7* pi/6) sin(7* pi/6)1 cos(8* pi/6) sin(8* pi/6)1 cos(9* pi/6) sin(9* pi/6)1 cos(10* pi/6) sin(10* pi/6)1 cos(11* pi/6) sin(11* pi/6)1 cos(12* pi/6) sin(12* pi/6)

A

B

C

55 39 84 7355 41 85 7359 45 83 7264 46 80 6968 55 77 6675 61 76 6481 64 75 6381 64 76 6477 61 75 6570 54 77 6663 49 79 6855 41 82 71

Paris Rio

hi/lo hi/lo

* =Aph Apl Arh Arl

Bph Bpl Brh Brl

Cph Cpl Crh Crl

Page 36: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Fitting a sine & cosine in MATLAB

Fitting a sine & cosine in MATLAB

We'd like M*[A;B;C] = temp':m = 1:12; % monthsCON = ones(12,1); % columns of MCOS = cos(2*pi/12 * m)';SIN = sin(2*pi/12 * m)';

fit = [CON COS SIN] \ temp' % best fit

clf; plot(temp', '+'); % plot datahold on; plot([CON COS SIN]*fit); % plot fit

Page 37: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Error in fitting sine & cosine

Error in fitting sine & cosine

We'd like M*[A;B;C] = temp':

We have fit

resid = M*fit-temp';error = sum(resid .*resid)Gives four errors, one for each city

Page 38: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

A space of facesA 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 space:A point (.5, -.2, .7) =

.5 x -.2 y +.7 z

A matrix is a set of rows or columns that define a vector space.

Page 39: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Space of facesSpace 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

Page 40: What Is a Matrix? COMP 116 August 29, 2007. Outline: What is a matrix?  A support for something growing  A table of numbers  A stack (column) of row

Average studentAverage 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