Presentation is loading. Please wait.

Presentation is loading. Please wait.

Vectors, matrices, and arithmetic

Similar presentations


Presentation on theme: "Vectors, matrices, and arithmetic"— Presentation transcript:

1 Vectors, matrices, and arithmetic
Can be found at: Intro to Matlab Vectors, matrices, and arithmetic Plotting Flow Constructs Other Useful Things

2 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

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! 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

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

7 More Matrix Math The ‘.’ operator Example:
“element by element” access Example: x = [1 2 3]; y = [4; 5; 6]; x * y = 32 x .* y = [ ] For some functions (x same as above): x ^ 2 ERROR! x . ^2 fine

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

9 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’)

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 (<condition>) <body> elseif
end WHILE block while (<condition>) <body> end Conditions same as C, ( ==, >=, <=) except != is ~=

12 More Flow Constructs FOR block for i = 1:10 <body> end
SWITCH statement switch <expression> case <condition>, <statement> otherwise <condition>, end

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

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)
eps machine epsilon ans most recent unassigned answer pi …. i and j Matlab supports imaginary numbers!

16 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

17 Questions?


Download ppt "Vectors, matrices, and arithmetic"

Similar presentations


Ads by Google