# Matlab tutorial and Linear Algebra Review

```Announcements
• Problem Set 2, handed out today, due next
Tuesday.
• Late Homework should be turned into my
office with date and time written on it.
• Mail problem sets in one zipped file to farrell
(at) cs.umd.edu. When emailing matlab code
to Ryan, use subject: CMSC 426 Matlab
code.
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:
/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
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
How do we prove these properties of the inner product? Let’s start with the fact that
orthogonal vectors have 0 inner product. Suppose one vector is (x,y), and WLOG
x,y&gt;0. Then, if we rotate that by 90 degrees counterclockwise, we’ll get (y, -x).
Rotating the vector is just like rotating the coordinate system in the opposite direction.
And (x,y)*(y,-x) = xy – yx = 0.
Next, note that v*w = (v*w)/(||v||||w||) * ||v||||w|| This means that if we can show that
when v and w are unit vectors v*w = cos alpha, then it will follow that in general v*w =
||v|| ||w|| cos alpha. So suppose v and w are unit vectors.
Next, note that if w1 + w2 = w, then v*w = v*(w1+w2) = v*w1 + v*w2. For any w, we
can write it as the sum of w1+w2, where w1 is perpendicular to v, and w2 is in the
same direction as v. So v*w1 = 0. v*w2 = ||w2||, since v*w2/||w2|| = 1. Then, if we just
draw a picture, we can see that cos alpha = ||w2|| = v*w2 = v*w.
Matrices
Anm
 a11 a12
a
 21 a22
 a31 a32


 
an1 an 2
 a1m 
 a2 m 
 a3m 

  
 anm 
Sum:
Cnm  Anm  Bnm
cij  aij  bij
A and B must have the same
dimensions
Matrices
Product:
Cn p  Anm Bm p
m
cij   aik bkj
k 1
Identity Matrix:
A and B must have
compatible dimensions
Ann Bnn  Bnn Ann
1 0  0


0 1  0
I 
IA

AI

A
   


0 0  1
Matrices
Transpose:
Cmn  A nm
cij  a ji
T
If
AT  A
( A  B)  A  B
T
T
( AB)T  BT AT
A is symmetric
T
Indexing into matrices
Files
Matlab
Functions
• Format: function o = test(x,y)
• Name function and file the same.
• Only first function in file is visible
outside the file.
• Look at sample function
Images
•
•
•
•
Black and white image is a 2D matrix.
Intensities represented as pixels.
Color images are 3D matrix, RBG.
Matlab
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.
• The inner product will be very important.
```