This document provides an introduction and overview of Matlab. It discusses (1) vectors, matrices and arithmetic operations in Matlab, (2) plotting capabilities, and (3) flow control structures like if/else statements and for loops. Some key aspects covered include how to define vectors and matrices, perform element-wise and standard matrix operations, create plots of data, and write programs with basic programming constructs. The document also provides examples of plotting commands and symbolic math operations in Matlab.
2. 2
Why use Matlab?
• Drawbacks:
Slow compared to C or Java
• Advantages:
Handles vector and matrices very nicely
Quick plotting and analysis
EXTENSIVE documentation (type ‘help’)
Lots of nice functions: FFT, fuzzy logic, neural
nets, numerical integration, OpenGL (!?)
Vectors and Matrices
• Can be run from 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 possible
• Can also use matrices / vectors as elements:
x = [1 2 3]
y = [ x 4 5 6]
3. 3
Some Standard matrices
• ones(3,3) 3x3 of all ones
• zeros(3,3) 3x3 of all zeros
• eye(3,3) 3x3 identity
• rand(3) 3x3 random elements
• 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
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
4. 4
Matrix Operations
• For matrix operations
– Dimensions must agree
• Scalar operations
– Same as usual
• Scalar / matrix mixed
– Scalar + matrix = [scalar + matrix(x, y)]
– Scalar * matrix = [scalar * matrix(x, y)]
More Matrix Operations
• 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 ^ 2 ERROR!
– x . ^2 fine
5. 5
More Matrix Operations
• x=1:12
• reshape(x, 3,4)
• a=2*ones(3,4)
• X.*a
• b=[1:3;1:3;1:3;1:3]
• X.*b
• y=reshape(x, 4,3)
• y.^b
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)
7. 7
More Plotting
• Graphics Window
– 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 default mode
• 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
8. 8
Flow Control
• IF block
if (<condition>)
<body>
elseif
<body>
end
• WHILE block
while (<condition>)
<body>
end
Conditions same as C, ( ==, >=, <=) except != is ~=
More Flow Control
• FOR block
for i = 1:10
<body>
end
• SWITCH statement
switch <expression>
case <condition>,
<statement>
otherwise <condition>,
<statement>
end
9. 9
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;
Useful Commands
• Single quote is transpose
• % same as // comment in C, Java
No /* block comments */
• ; suppresses printing
• More:
max(x) min(x)
mean(x) median(x)
abs(x) dot(x,y)
cross(x,y) flops (flops in this session)
10. 10
Useful Constants
• Inf infinity
• NaN Not a number (div by zero)
• eps machine epsilon (precision)
• ans most recent unassigned answer
• pi 3.14159….
• i and j 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;
• Matlab is optimized for vectorization
11. 11
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