Professional Documents
Culture Documents
Computer-equipped classroom
• Complete list available at
http://www.cia.polimi.it/servizi/software.html (then select wished
Matlab released
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
3 4
Matlab Documentation Matlab overview
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
5 6
Matlab overview Matlab overview
Platforms Toolboxes
Matlab woks on several platforms MATLAB features a family of add-on application-specific solutions
• PCs powered by Windows (provided by www.cia.polimi.it) called toolboxes.
• Unix/Linux Systems (provided by www.cia.polimi.it) Toolboxes are comprehensive collections of MATLAB functions (M-
• Macintosh (not provided)
files) that extend the MATLAB environment to solve particular classes
of problems.
They cover many areas:
Similar GUI and interface
• signal processing,
• control systems,
• neural networks,
• fuzzy logic,
• wavelets,
• simulation…
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
7 8
Matlab overview Matlab Desktop
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
9 10
Matlab Desktop Matlab Desktop
Command History
All executed command
are listed here and can be
Workspace
recalled (re-executed)
All stored variables are
when needed by double
shown here together with
clicking
their current values. They
can be also edited.
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
11 12
Matlab Desktop Matlab helpdesk
Current Directory
Current working directory,
Help
Help on line
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
13 14
Matlab online help Variables
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
15 16
Variables Matrices
Variables are
added to the
workspace. The Semicolon at the
values are shown end of commands
and can be suppresses output
manually edited by
double-clicking
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
17 18
Matrices Vectors
Row vector
Column vector
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
19 20
Working with matrices: Example Working with matrices: Example
Transpose of A
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
21 22
Matrix elements Colon operator
To access to single elements of matrix use A(r,c) notation, where r The colon “:” operator is the most important MATLAB operator
is the index of row and c index of column Use starting_value:increment:end_value
C-like programmers, watch out! it returns a vector containing all values from starting_value to
• The first element (row or column) has index 1 end_value with step increment
When assigning a
value, if index
exceeded MATLAB
automatically
increases dimensions
to place the new
element, putting to 0
all the other new
elements
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
23 24
Colon operator Operators
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
25 26
Addition example Multiplication example
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
27 28
Element-wise operators Example
Elementwise division
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
29 30
Concatenation Deleting row and columns
Concatenation is the process of joining small matrices to make bigger The pair of square brackets “[]” can be used to delete rows or columns
ones. of a matrix
The pair of square brackets, [], is the concatenation operator.
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
31 32
Other functions Example
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
33 34
Special matrices Solving linear systems
eye(n) returns the n-by-n identity matrix Linear systems in matrix form can be solved using operator “\”
Given Ax=b with x vector of unknown, the system can be solved as
x=A\b which corresponds to x = inv(A) * b
rand(n,m) returns the n-by-m matrix of random numbers If the equation Ax = b does not have a
solution (and A is not a square matrix), x =
A\b returns a least squares solution
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
35 36
Using MATLAB as a programming language MATLAB m-file editor
Use M-file to create script or function that can be called from the
command prompt
Script are sequence of function/command call that can be executed in
batch calling the m-file
M-file is a normal text file that contains the sequence of commands to
be execute
To execute a M-file simply call the name of the m-file from command
prompt
M-file can be edited using the provide editor of MATLAB, but you can
use any text editor you like
MATLAB provide flow control structures (if, for, while ecc) that can be
used in a m-file
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
37 38
Example – Calling a m-file Flow control – if…else and elseif
if expression1
statements1
elseif expression2
statements2
elseif expression3
statements3
…
else
statements4
end
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
39 40
Flow control – conditional expression Flow control - switch
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
41 42
Flow control - for Flow control - while
Example
[r c] = size(A) Similar to C
for m = 1:r
for n = 1:c For and while inside the loop allow the use of:
A(m,n) = 1/(m+n -1);
end • continue to pass the control to the next iteration
end • break to exit early from the loop
The counter variables are managed automatically by MATLAB inside
the loop!
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
43 44
Function Function example
Functions are stored in m-file named with the same name of the
function
Declaration:
function [y1, ... , yn] = nome_funzione (x1, ... , xm)
Where
• x1, ... , xm are input params (optional)
• [y1, ... , yn] are output params (optional)
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
45 46
Function example Graphics
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
47 48
Graphics Graphics
Function plot allow to create 2D graphics in MATLAB To plot different graphics on the same reference frame
plot(X,Y) plots the columns of X versus the column of Y plot(x1,y1, x2,y2, x3,y3, x4,y4,…)
Or:
hold on
plot(x1,y1)
plot(x2,y2)
plot(x3,y3)
plot(x4,y4)
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
49 50
Graphics Graphics - example
Plot style
plot(x1…, ‘options’)
Option is a string delimited by ‘ containing the specifiers for line styles
plot(x1,y1, ‘:ro’)
Plot a red (r) dotted (:) line
placing a circle (o) on each point
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
51 52
Images Images
In MATLAB images are matrices where each element store the value load and display an image
of the correspondent pixel
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
53 54
Working with images - example
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
55 56
Fundamental matrix: example
x'T Fx = 0
x' xf11 + x' yf12 + x' f13 + y' xf21 + y' yf22 + y' f 23 + xf31 + yf32 + f 33 = 0
[x' x, x' y, x' , y' x, y' y, y' , x, y,1][ f11, f12 , f13 , f 21, f 22 , f 23 , f31, f32 , f33 ]T = 0
(data) (unknowns)
(linear)
min Af
2
=0 ⎡ f11 ⎤
f =1 ⎢f ⎥
⎢ 12 ⎥
⎢ f 13 ⎥
= f T AT Af
2
since Af ⎡ x1 x1´ y1 x1´ x1´ x1 y1´ y1 y1´ y1´ x1 y1 1⎤ ⎢ ⎥
⎢x x ´ y x ´ x ´ x y ´ y y ´ y ´ x y 1⎥ ⎢ f 21 ⎥
⎢ 2 2 2 2 2 2 2 2 2 2 2 2 ⎥⎢ f ⎥ = 0
this requires to find the eigenvector associated to the ⎢ # # # # # # # # # ⎥ ⎢ 22 ⎥
smallest eigenvalue of ATA9x9 (e.g. svd) ⎢ ⎥ ⎢ f 23 ⎥
⎣ x n x n ´ y n x n ´ x n ´ x n y n ´ y n y n ´ y n ´ x n y n 1⎦ ⎢ f ⎥
⎢ 31 ⎥
~10000 ~10000 ~100 ~10000 ~10000 ~100 ~100 ~100 1 ⎢ f ⎥
Due to data noise the constraint rank(F)=2 is not 32
enforced Orders of magnitude difference ⎢ ⎥
! ⎣ 33 ⎦
f
Between column of data matrix
Use svd on F and set the smallest singular value to 0 → least-squares yields poor results
Artificial Intelligence and Robotics Laboratory Artificial Intelligence and Robotics Laboratory
59
Fundamental matrix: example