Vectors, matrices, and arithmetic Can be found at: http://www.cs.unc.edu/~kim/matlab.ppt Intro to Matlab Vectors, matrices, and arithmetic Plotting Flow Constructs Other Useful Things
Why use Matlab? Advantages: Handles vector and matrices very nice Quick plotting and analysis EXTENSIVE documentation (type ‘help’) Lots of nice functions: FFT, fuzzy logic, neural nets, numerical integration, OpenGL (!?) Drawbacks: Slow compared to C or Java
Vectors and Matrices Can be to command line or from *.m file scalar: x = 3 vector: x = [1 0 0] 2D matrix: x = [1 0 0; 0 1 0; 0 0 1] arbitrarily higher dimensions (don’t use much) Can also use matrices / vectors as elements: x = [1 2 3] y = [ x 4 5 6]
Common matrices to use ones(3,3) 3x3 of all ones zeros(3,3) 3x3 of all zeros eye(3,3) 3x3 identity (har har har) linspace(1,10,100) linear spacing from 1 to 10, with 100 spacings (also logspace) x = 1:10 linear spacing from 1 to 10, counting by 1
Element access MATLAB IS NOT ZERO INDEXED! x retrieves entire matrix x x(1,2) retrieves element at row 1, col 2 x(1, 5:10) retrieves row 1, columns 5 to 10 x(1,:) retrieves row 1, all columns Useful functions: length(x) length of vector x (cols) size(x) rows, cols of x
Matrix Math Matrix math Scalar math Scalar / matrix math Dimensions must agree Scalar math Same as usual Scalar / matrix math Scalar + matrix = [scalar + matrix(x, y)] Scalar * matrix = [scalar * matrix(x, y)]
More Matrix Math The ‘.’ operator Example: “element by element” access Example: x = [1 2 3]; y = [4; 5; 6]; x * y = 32 x .* y = [4 10 18] For some functions (x same as above): x ^ 2 ERROR! x . ^2 fine
Plotting 2D graphing Example: BUT: x = linspace(-10,10,100) y = x .^2 plot(x,y) Example: x = linspace(-10,10,100) y = x .^2 BUT: z = x .^3 plot(x,z)
More Plotting Old plot got stomped Multiple data sets: To open a new graph, type ‘figure’ Multiple data sets: Type ‘hold on’ to add new plot to current graph Type ‘hold off’ to resume stomping Make your graph beautiful: title(‘apples over oranges’) xtitle(‘apples’) ytitle(‘oranges’)
3D Plotting 3D plots – plot an outer product x = 1:10 y = 1:10 z = x’ * y mesh(x,y,z) Single quote ‘ means transpose (can’t cut and paste though…)
Flow Constructs IF block if (<condition>) <body> elseif end WHILE block while (<condition>) <body> end Conditions same as C, ( ==, >=, <=) except != is ~=
More Flow Constructs FOR block for i = 1:10 <body> end SWITCH statement switch <expression> case <condition>, <statement> otherwise <condition>, end
Other Language Features Matlab language is pretty sophisticated Functions Stored in a *.m file of the same name: function <return variable> = <function name>(<args>) <function body> Structs point.x = 2; point.y = 3; point.z = 4; Even has try and catch (never used them)
Useful Commands Single quote is transpose % same as // comment in C, Java No /* block comments */ (annoying) ; suppresses printing More: max(x) min(x) mean(x) median(x) abs(x) dot(x,y) cross(x,y) flops (flops in this session)
Useful Constants Inf infinity NaN Not and number (div by zero) eps machine epsilon ans most recent unassigned answer pi 3.14159…. i and j Matlab supports imaginary numbers!
Final note Wrong: Right: Matlab is optimized for vectorization for x = 1:10 for y = 1:10 foo(x,y) = 2 * bar(x,y) end Right: foo = 2 * bar; Matlab is optimized for vectorization
Questions?