Arab Open University Faculty of Computer Studies M133...
Transcript of Arab Open University Faculty of Computer Studies M133...
MATLAB
This introduction will give
• Some basic ideas
• Main advantages and drawbacks compared
to other languages
MATLAB
What Is MATLAB?
MATLAB (MATrix LABoratory)
• High-performance language for technical computing
• Computation, visualization, and programming in an
easy-to-use environment
Typical uses include:
• Math and computation
• Algorithm development
• Modelling, simulation and prototyping
• Data analysis, exploration and visualization
• Scientific and engineering graphics
• Application development, including Graphical User
Interface building
MATLAB
MATLAB is an INTERPRETED language
…with all the advantages and drawbacks !
…there is a MATLAB Compiler, but it‟s not worth too
much in terms of speed
MATLAB takes care of the usual programming
overhead, e.g. memory allocation, data type
conversion, parameters by value/reference
… with all the advantages and drawbacks !
MATLAB is NOT object oriented, although there are
some attempts to extend it
Why MATLAB
A good choice for vision program development
because:
• Easy to do very rapid prototyping
• Easy to learn, well documented
• A good library of image processing functions
• Excellent display capabilities
• Widely used for teaching and research in
universities and industry
• Another language to impress your boss with !
Why not MATLAB
Has some drawbacks:
• Slow for some kinds of processes
• Not geared to the web
• Not designed for large-scale system
development
MATLAB Components
MATLAB consists of:
• The MATLAB language
a high-level matrix/array language with control flow statements,
functions, data structures, input/output, and object-oriented
programming features.
• The MATLAB working environment
the set of tools and facilities that you work with as the MATLAB
user or programmer, including tools for developing, managing,
debugging, and profiling
• Handle Graphics
the MATLAB graphics system. It includes high-level commands
for two-dimensional and three-dimensional data visualization,
image processing, animation, and presentation graphics.
• …(cont‟d)
MATLAB Components
…
• The MATLAB function library
a vast collection of computational algorithms ranging from
elementary functions like sum, sine, cosine, and complex
arithmetic, to more sophisticated functions like matrix inverse,
matrix eigenvalues, Bessel functions, and fast Fourier
transforms as well as special image processing related
functions
• The MATLAB Application Program Interface (API)
a library that allows you to write C and Fortran programs that
interact with MATLAB. It include facilities for calling routines
from MATLAB (dynamic linking), calling MATLAB as a
computational engine, and for reading and writing MAT-files.
MATLAB
Some facts for a first impression
• Everything in MATLAB is a MATRIX !
• Again: MATLAB is an interpreted language, no
compilation needed (but possible)
• Again: MATLAB does not need any variable
declarations, no dimension statements, has no
packaging, no storage allocation, no pointers
• Programs can be run step by step, with full access to
all variables, functions etc.
What does MATLAB code look like?
What‟s the output ?
A simple example:
a = 1
while length(a) < 10
a = [0 a] + [a 0]
end
What does MATLAB code look like?
A simple example:
a = 1
while length(a) < 10
a = [0 a] + [a 0]
end
which prints out Pascal‟s triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
(with “a=” before each line).
What does MATLAB code look like?
Another simple example:
t = 0:pi/100:2*pi;
y = sin(t);
plot(t,y)
Remember:
EVERYTHING IN MATLAB
IS A MATRIX !
creates 1 x 200 Matrix
Argument and result: 1 x 200 Matrix
Matrices
• Rows and columns are always numbered
starting at 1
• MATLAB matrices are of various types to
hold different kinds of data (usually floats or
integers)
• A single number is really a 1 x 1 matrix in
MATLAB!
• MATLAB variables are not given a type,
and do not need to be declared
• Any matrix can be assigned to any variable
Matrices
Building matrices with [ ]:
A = [2 7 4]
B = [2; 7; 4] Or B = A′
C = [2 7 4; 3 8 9]
D = [ C C ]
2 7 4
2
7
4
2 7 4
3 8 9
?
Matrices
Building matrices with [ ]:
A = [2 7 4]
B = [2; 7; 4] Or B = A′
C = [2 7 4; 3 8 9]
D = [ C C ]
2 7 4
2
7
4
2 7 4
3 8 9
2 7 4
3 8 9
2 7 4
3 8 9
Polynomials as Row Matrices
>> p1 = [1 -10 35 -50 24] % specify the coefficients of p1(x)
p1 =
1 -10 35 -50 24
>> roots_p1 = roots(p1) % Find the roots of p1
roots_p1 =
4.0000
3.0000
2.0000
1.0000
Roots of the polynomial:
Consider
p1(x) = x4 – 10x3 + 35x2 – 50x + 24
Polynomials as Row Matrices
>> r = [1 2 3 4] % specify the roots of the polynomial
r =
1 2 3 4
>> p1 = poly(r) % Find the polynomial coefficients
p1 =
1 -10 35 -50 24
>> a = polyval(p1,1) % Evaluate p1 at x = 1
a =
0
Polynomial construction from known roots
Polynomials as Row Matrices
>> p1 = [1 -3 0 5 7 9];
>> p2 = [2 0 -8 0 4 10 12];
>> p1p2 = conv(p1,p2) % Product of p1(x) and p2(x)
p1p2 =
2 -6 -8 34 18 -24 -74 -88 78 166 174 108
>> p3 = polyder(p2) % Compute the derivative of p2(x)
p3 =
12 0 -32 0 8 10
Product of the polynomials:
p1(x) = x5 – 3x4 + 5x2 + 7x + 9
p2(x) = 2x6 – 8x4 + 4x2 + 10x + 12
Polynomials as Row Matrices
>> p1 = [1 0 -3 0 5 0 7 9];
>> p2 = [2 -8 0 0 4 10 12];
>> [q1,r1] = deconv(p1,p2) % Divide p1(x) by p2(x) to find the
quotient q1(x) and the remainder r1(x)
q1 =
0.5000
r1 =
4 -3 0 3 2 3
Division of the polynomials:
Matrices
Some operators must be handled with care:
A = [1 2 ; 4 5]
B = A * A prints 9 12
24 33
B = A .* A prints 1 4
16 25
Element by element multiplication
Submatrices
A matrix can be indexed using another matrix, to
produce a subset of its elements:
a = [100 200 300 400 500 600 700] b = [3 5 6]
c = a(b):
300 500 600
Submatrices
To get a subsection of a matrix, we can produce the
index matrix with the colon operator:
a(2:5)
prints
ans = 200 300 400 500
• This works in 2-D as well, e.g. c(2:3, 1:2) produces a
2 x 2 submatrix.
• The rows and columns of the submatrix are
renumbered.
loops
„for‟ loops in MATLAB iterate over matrix elements:
b = 0
for i = [ 3 9 17]
b = b + i;
end
Result: 29
Note:
THIS IS NOT THE WAY YOU SHOULD
PROGRAM A SUM, SINCE LOOPS ARE SLOW
IN MATLAB !
loops
„for‟ loops in MATLAB iterate over matrix elements:
b = 0
for i = [ 3 9 17]
b = b + i;
end
Result: 29
The MATLAB way to write that program would have been:
b = sum([ 3 9 17]);
Avoid loops if possible !
% Example for runtime difference of loops and built in functions
a=rand(10000,1000); % Matrix with 10000000 (1e7) elements
tic
theSum=0;
for i=1:size(a,1)
for j=1:size(a,2)
theSum = theSum + a(i,j);
end
end
toc
fprintf('result of loop: %f\n',theSum);
tic
theSum=sum(a(:));
toc
fprintf('result of loop: %f\n',theSum);
-----------------------------------------------
Elapsed time is 0.203000 seconds.
result of loop: 4999093.175950
Elapsed time is 0.078000 seconds.
result of loop: 4999093.175950
loops
Here: Speed up factor 2.6
loops
The typical „for‟ loop looks like:
for i = 1:6
…
end
Which is the same as:
for i = [1 2 3 4 5 6]
…
end
loops
Or a bit more sophisticated:
for i = 1:2:7
…
End
Which is the same as:
for i = [1 3 5 7]
…
end
Avoiding loops
How to avoid loops:
Task:
count the elements being greater than 5 in the
following matrix M:
2 7 4
3 8 9
2 7 4
3 8 9
The non-MATLAB way:
counter=0
For rows=1:2
For columns=1:6
if M(rows,columns) > 5
inc counter
end
end
Avoiding loops
Another example, showing the mighty
INDEXING possibilities:
Task: eliminate the whitespaces in the following
vector M
Avoiding loops
A B C D A B C D
i = find (M ~= „ „); % result: i = [1 3 4 6]
M = M(i);
Or shorter:
M = M(find(M ~= „ „));
Avoiding loops
A B C D A B C D
survivingSubSegs = pointDensity >= minDensity;
diffSubSegs = diff([0 survivingSubSegs 0]);
startSeg = find(diffSubSegs == 1);
endSeg = find(diffSubSegs == -1) - 1;
Avoiding loops
An example how to work without loops,
taken from the EMSF algorithm for line detection
What’s the purpose ?
Conclusion
• MATLAB is a mighty tool to manipulate
matrices
• Images can be treated as matrices
• MATLAB is a mighty tool to manipulate
images
In my opinion…
• MATLAB should be used to code software
prototypes
• Research is mostly about prototypes, not
runtime-optimized software
• MATLAB should be used in research
In my opinion…
• MATLAB prototypes must be re-coded (e.g. in
C++) if there‟s need for speed
• Algorithm development time is drastically
shorter in MATLAB
How to go on
MATLAB is exceptionally well documented:
Opening
help/MATLAB help/Getting started
help/MATLAB help/Using MATLAB
Offers help to the environment, programming
concepts, function libraries etc.
How to go on
The „help <functionname>‟ command shows the
first comment-lines of the function given by the
m-file <functionname>.
„Doc‟ does the same, but nicer (html-style,
graphics, examples)
How to go on
The TAB key automatically extends commands
given. This is sometimes helpful if the function
name should be obvious:
• Task: sort the rows of the matrix A
• Idea: there could be a function named „sortXXXX‟
• Type: „sort + TAB‟
• Result: sort, sortcellchar, sorted, sortiv, sortrows, sortrowsc
How to go on
Result: sort, sortcellchar, sorted, sortiv, sortrows, sortrowsc
This looks promising !
„help sortrows‟ will exclude doubt:
SORTROWS Sort rows in ascending order.Y = SORTROWS(X) sorts the rows of the matrix X in ascending
order as a group. X is a 2-D numeric or char matrix. For a char
matrix containing strings … (+usage, examples etc.)
How to go on
Another helpful command is „lookfor‟:
„lookfor <topic>‟ looks for the string <topic> in all
first comments lines (the ones shown by the
„help‟ command) of ALL functions, returning
functions with a hit.
e.g. „lookfor sort‟:
How to go on
e.g. „lookfor sort‟:
bestof.m: %BESTOF(index,names,M,n,<show>) returns the sorted comparison
values of polygon(index) + filenames
CPLXPAIR Sort numbers into complex conjugate pairs.
ISSORTED True for sorted vector.
SORT Sort in ascending order.
SORTROWS Sort rows in ascending order.
RTMDLSORTFLDS is a RTW support function.
DSORT Sort complex discrete eigenvalues in descending order.
ESORT Sort complex continuous eigenvalues in descending order.
DSPBLKSORT DSP Blockset sort block helper function.
UISORTDATA GUI for sorting matrices by row
XREGLVSORTER Function to sort items in listview
sortiv.m: % function [out,err] = sortiv(in,sortflg,nored,epp)
EIGFUN Function to return sorted eigenvalues (used in GOALDEMO).
V2SORT Sorts two vectors and then removes missing elements.…
…
How to go on
Note:
…a creative search using the right topics with
„lookfor‟ can save a lot of time. MATLAB comes
with mostly all basic things you can think of.
Search before you program !
… a Google search „MATLAB +topic‟ is
sometimed an unbelievably satisfying source of
time-saving program collections.
How to go on
Now:
• Browse the documentation !
• Read sample codes !
• Simply browse the list of functions (doc)
• Try and type some lines, use the graphics !
Some Tips
http://mathworks.com/academia/student_center/tut
orials/launchpad.html
http://courses.csail.mit.edu/6.869/resources.html
http://www.math.siu.edu/matlab/tutorials.html