L17 - Matlab Uses For Image
L17 - Matlab Uses For Image
Introduction to Matlab
1. Vectors, matrices, and arithmetic 2. Plotting 3. Flow Constructs 4. Other Useful Things
x = 1:10
linear spacing from 1 to 10, counting by 1
Accessing elements
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 Operations
For matrix operations
Dimensions must agree
Scalar operations
Same as usual
Example:
x = [1 2 3]; y = [4; 5; 6]; x * y = 32 x .* y = [4 10 18]
Plotting
2D graphing
plot(x,y)
Example:
x = linspace(-10,10,100) y = x .^2 plot(x,y)
Also:
z = x .^3 plot(x,z)
Plotting Example
basis = [ [1 0]; [0 0]; [0 1]; ]' ; square = [ [ 1 1] [-1 1]; [-1 -1]; [ 1 -1]; [ 1 1];
]' ;
axis equal; plot( sqrt(3), sqrt(3), ' w.' ); plot( -sqrt(3), -sqrt(3), ' w.' ); plot( basis(1,:), basis(2,:), ' k' ); plot( square(1,:), square(2,:), ' b' );pause obj = S*square; plot( obj(1,:), obj(2,:), ' r' );pause
basis = [ [1 0]; [0 0]; [0 1]; [1 0]; ]' ; square = [ [ 1 1]; [-1 1]; [-1 -1]; [ 1 -1]; [ 1 1];
Plotting Example
More Plotting
Graphics Window
To open a new graph, type figure
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
Flow Control
IF block if (<condition>) <body> elseif <body> end WHILE block while (<condition>) <body> end
Structs
point.x = 2; point.y = 3; point.z = 4;
Useful Commands
Single quote is transpose % same as // comment in C, Java No /* block comments */ ; suppresses printing More:
max(x) mean(x) abs(x) cross(x,y) min(x) median(x) dot(x,y) flops (flops in this session)
Useful Constants
Inf NaN eps ans pi i and j infinity Not a number (div by zero) machine epsilon (precision) most recent unassigned answer 3.14159. Matlab supports imaginary numbers!
Programming
Wrong:
for x = 1:10 for y = 1:10 foo(x,y) = 2 * bar(x,y) end end
Right:
foo = 2 * bar;
10
Symbolic Maths
Symbolic mathematics can be done using Matalb:
a = sqrt(sym(2)) a= 2^(1/2) th=sym(' th' ); rt=sym([cos(th) sin(th) 0;-sin(th) cos(th) 0;0 0 1]); rt = [ cos(th), sin(th), 0] [ -sin(th), cos(th), 0] [ 0, 0, 1]
Good luck
11