Matlab tutorial and Linear Algebra Review • • • • • • • Today’s goals: Learn enough matlab to get started. Review some basics of Linear Algebra Essential for geometry of points and lines. But also, all math is linear algebra. (ok slight exaggeration). Many slides today adapted from Octavia Camps, Penn State. Starting Matlab • For PCs, Matlab should be a program. • For Sun’s: Numerical Analysis and Visualization Matlab 6.1 Help • help • help command Eg., help plus • Help on toolbar • demo • Tutorial: http://amath.colorado.edu/scico/tutorials /matlab/ Matlab interpreter • Many common functions: see help ops Vectors v ( x1 , x2 , , xn ) • Ordered set of numbers: (1,2,3,4) v • Example: (x,y,z) coordinates of pt in If space. n 2 i 1 i x v 1, v is a unit vecto r Indexing into vectors Vector Addition v w ( x1 , x2 ) ( y1 , y2 ) ( x1 y1 , x2 y2 ) v V+w w Scalar Product av a( x1 , x2 ) (ax1 , ax2 ) av v Operations on vectors • sum • max, min, mean, sort, … • Pointwise: .^ Inner (dot) Product v w v.w ( x1 , x2 ).( y1 , y2 ) x1 y1 x2 . y2 The inner product is a SCALAR! v.w ( x1 , x2 ).( y1 , y2 ) || v || || w || cos v.w 0 v w Matrices Anm a11 a12 a 21 a22 a31 a32 an1 an 2 a1m a2 m a3m anm Sum: Cnm Anm Bnm cij aij bij A and B must have the same dimensions Matrices Product: Cn p Anm Bm p m cij aik bkj k 1 Identity Matrix: A and B must have compatible dimensions Ann Bnn Bnn Ann 1 0 0 0 1 0 I IA AI A 0 0 1 Matrices Transpose: Cmn A nm cij a ji T If AT A ( A B) A B T T ( AB) B A T A is symmetric T T T Matrices Determinant: A must be square a11 a12 a11 a12 det a11a22 a21a12 a21 a22 a21 a22 a11 a12 det a21 a22 a31 a32 a13 a22 a23 a11 a32 a33 a23 a33 a12 a21 a23 a31 a33 a13 a21 a22 a31 a32 Matrices Inverse: Ann A A must be square 1 nn A 1 1 nn Ann I a11 a12 1 a a a11a22 a21a12 21 22 a22 a12 a a 21 11 Indexing into matrices Euclidean transformations 2D Translation P’ t P 2D Translation Equation ty P P’ t y x P ( x, y ) t (t x , t y ) tx P' ( x t x , y t y ) Pt 2D Translation using Matrices ty P P’ t y x tx P ( x, y ) t (t x , t y ) t x x t x 1 0 t x P' y y t y 0 1 t y 1 P Scaling P’ P Scaling Equation P’ s.y P y P ( x, y ) P' ( sx, sy ) P' s P x s.x sx s 0 x P' sy 0 s y P' S P S Rotation P P’ Rotation Equations Counter-clockwise rotation by an angle Y’ x' cos y ' sin P’ P y X’ x P' R.P sin x cos y Degrees of Freedom x' cos y ' sin R is 2x2 sin x cos y 4 elements BUT! There is only 1 degree of freedom: The 4 elements must satisfy the following constraints: R RT RT R I det( R ) 1 Stretching Equation P ( x, y ) P ' ( s x x, s y y ) P’ Sy.y sx x sx P' s y 0 y P y x Sx.x 0 x sy y S P' S P Stretching = tilting and projecting (with weak perspective) sx x sx P' s y 0 y sx 0 x s s s y y y y 0 0 x y 1 Linear Transformation a b x P' c d y sin cos sin sy cos SVD cos s x 0 sin sx cos sy sin 0 0 sin s y cos 0 sin cos 1 cos x sin y cos x sin y Affine Transformation x a b tx P' y c d ty 1 Files Functions • Format: function o = test(x,y) • Name function and file the same. • Only first function in file is visible outside the file. Images Debugging • Add print statements to function by leaving off ; • keyboard • debug and breakpoint Conclusions • Quick tour of matlab, you should teach yourself the rest. We’ll give hints in problem sets. • Linear algebra allows geometric manipulation of points. • Learn to love SVD.