Colorado School of Mines Computer Vision Professor William Hoff Dept of Electrical Engineering &Computer Science Colorado School of Mines Computer Vision http://inside.mines.edu/~whoff/ 1 Introduction to Matlab Colorado School of Mines Computer Vision Matlab Introduction • Overview – “Matrix Laboratory” – An interactive programming environment – http://www.mathworks.com • Availability – On computers in BB 316, 304, 305 (you need an “adit” logon) – Student version available • Why we are using – Easy to prototype – Powerful toolboxes such as the image processing toolbox – Widely used; good help and documentation Colorado School of Mines Computer Vision 3 Matlab • Programming – – – – Can type on command line, or use a program file (“m”‐file) Semicolon at end of line is optional (suppresses printing) Control flow (if, for, etc) similar to C Differences from C: no variable declarations, no pointers • Help – Help browser ‐ best source – Command line – Web (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.s html) – Pointers to tutorials: http://people.duke.edu/~hpgavin/matlab.html Colorado School of Mines Computer Vision 4 Matlab • Everything is a matrix – a variable is a 1x1 matrix • Initializing a matrix: – Example: my_matrix = [1 2 3; 4 5 6; 7 8 9]; • Accessing a matrix (row, column): – my_matrix(1,2) has the value 2 • Colon operator generates a range my_matrix = 1 2 3 4 5 6 7 8 9 – Example: 1:10 = [1 2 3 4 5 6 7 8 9 10] – mytest(1, 2:4) is equivalent to mytest(1,[2 3 4]) – mytest(3, :) refers to all elements of row 3 Colorado School of Mines Computer Vision 5 Matlab • Built‐in functions (exp, sin, log, etc) • Variables – whos (view all variables) – clear all (Clear all variables ) • Types – double (default) – Also have integer, unsigned integer, logical, complex • Expressions – – – – • +,‐,/,* Power is ^ Transpose is ‘ (apostrophe) Period indicates point‐by‐point operation Plotting example (sin) Colorado School of Mines Computer Vision 6 Exercises • Create a vector A, consisting of the first 5 odd numbers • Create a vector B, consisting of the first 5 even numbers • Find the inner (dot) product of A and B Colorado School of Mines Computer Vision 7 Exercises • Find C, the outer product of A and B – what is the size of C ? • Compute the sum of all the elements of C • Compute the trace of C • Compute the determinant of C • Square all the elements of C Colorado School of Mines Computer Vision 8 Exercises • Create a “magic square” matrix M, having the same size as C (see help on the function magic) • Compute the matrix product of M and C • Compute the point‐by‐point product of M and C • Compute the product of M and (column) vector A Colorado School of Mines Computer Vision 9 Exercises • Write a program (using “for” loops) to compute the first 100 Fibonacci numbers – Recall the recurrence relation to generate them: • F0 = 0, F1 = 1 • Fn = Fn‐1 + Fn‐2 Colorado School of Mines Computer Vision 10 Exercises • Plot x vs ln(x), for values of x from 0 to 10 Colorado School of Mines Computer Vision 11