Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Matlab 1.Vectors, matrices, and arithmetic 2.Plotting 3.Flow Constructs 4.Other Useful Things Can be found at:http://www.cs.unc.edu/~kim/matlab.ppt.

Similar presentations


Presentation on theme: "Intro to Matlab 1.Vectors, matrices, and arithmetic 2.Plotting 3.Flow Constructs 4.Other Useful Things Can be found at:http://www.cs.unc.edu/~kim/matlab.ppt."— Presentation transcript:

1 Intro to Matlab 1.Vectors, matrices, and arithmetic 2.Plotting 3.Flow Constructs 4.Other Useful Things Can be found at:http://www.cs.unc.edu/~kim/matlab.ppt

2 Why use Matlab? Drawbacks: Slow compared to C or Java 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 (!?)

3 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]

4 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

5 Element access MATLAB IS NOT ZERO INDEXED! xretrieves 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

6 Matrix Math 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)]

7 More Matrix Math The ‘.’ operator –“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 ^ 2ERROR! –x. ^2fine

8 Plotting 2D graphing plot(x,y) Example: x = linspace(-10,10,100) y = x.^2 plot(x,y) BUT: z = x.^3 plot(x,z)

9 More Plotting Old plot got stomped –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’)

10 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…)

11 Flow Constructs IF block if ( ) elseif end WHILE block while ( ) end Conditions same as C, ( ==, >=, <=) except != is ~=

12 More Flow Constructs FOR block for i = 1:10 end SWITCH statement switch case, otherwise, end

13 Other Language Features Matlab language is pretty sophisticated –Functions Stored in a *.m file of the same name: function = ( ) –Structs point.x = 2;point.y = 3;point.z = 4; –Even has try and catch (never used them)

14 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)

15 Useful Constants Inf infinity NaN Not and number (div by zero) epsmachine epsilon ansmost recent unassigned answer pi3.14159…. i and jMatlab supports imaginary numbers!

16 Final note Wrong: 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

17 Questions?


Download ppt "Intro to Matlab 1.Vectors, matrices, and arithmetic 2.Plotting 3.Flow Constructs 4.Other Useful Things Can be found at:http://www.cs.unc.edu/~kim/matlab.ppt."

Similar presentations


Ads by Google