Transcript
8/4/2019 Cis601 02 Matlab Intro
1/57
MATLAB
8/4/2019 Cis601 02 Matlab Intro
2/57
MATLAB
This introduction will give
Some basic ideas
Main advantages and
drawbacks compared toother languages
8/4/2019 Cis601 02 Matlab Intro
3/57
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
8/4/2019 Cis601 02 Matlab Intro
4/57
MATLAB
Matlab is an INTERPRETED languagewith all the advantages and drawbacks !
there is a Matlab Compiler, but its not
worth too much in terms of speed
Matlab takes care of the usual programmingoverhead, e.g. memory allocation, datatypeconversion, parameters by value/reference with all the advantages and drawbacks !
Matlab is NOT object oriented, although
there are some attempts to extend it
8/4/2019 Cis601 02 Matlab Intro
5/57
MATLAB
Matlab is able to cooperate with otherlanguages, e.g.
C, C++
JAVA Fortran
8/4/2019 Cis601 02 Matlab Intro
6/57
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 !
8/4/2019 Cis601 02 Matlab Intro
7/57
Why not MATLAB
Has some drawbacks:
Slow for some kinds of processes Not geared to the web Not designed for large-scale system
development
8/4/2019 Cis601 02 Matlab Intro
8/57
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, andprofiling
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.
(contd)
8/4/2019 Cis601 02 Matlab Intro
9/57
MATLAB Components
The MATLAB function library. a vast collection of computational algorithms ranging from elementary
functions like sum, sine, cosine, and complex arithmetic, to moresophisticated functions like matrix inverse, matrix eigenvalues, Bessel
functions, and fast Fourier transforms as well as special image processingrelated 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 andwriting MAT-files.
8/4/2019 Cis601 02 Matlab Intro
10/57
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 variabledeclarations, no dimension statements, has no
packaging, no storage allocation, no pointers
Programs can be run step by step, with fullaccess to all variables, functions etc.
8/4/2019 Cis601 02 Matlab Intro
11/57
What does Matlab code look like?
A simple example:
a = 1while length(a) < 10a = [0 a] + [a 0]end
Whats the output ?
8/4/2019 Cis601 02 Matlab Intro
12/57
What does Matlab code look like?
A simple example:
a = 1while length(a) < 10a = [0 a] + [a 0]end which prints out Pascals triangle:
1
1 11 2 11 3 3 11 4 6 4 11 5 10 10 5 11 6 15 20 15 6 11 7 21 35 35 21 7 11 8 28 56 70 56 28 8 11 9 36 84 126 126 84 36 9 1
(with a= before each line).
8/4/2019 Cis601 02 Matlab Intro
13/57
What does Matlab code look like?
Another simple example :
t = 0:pi/100:2*pi;y = sin(t);plot(t,y)
8/4/2019 Cis601 02 Matlab Intro
14/57
What does Matlab code look like?
Another simple example:
t = 0:pi/100:2*pi;y = sin(t);plot(t,y)
Remember:
EVERYTHING IN MATLABIS A MATRIX !
creates 1 x 200 Matrix
Argument and result: 1 x 200 Matrix
8/4/2019 Cis601 02 Matlab Intro
15/57
Matrices
!
8/4/2019 Cis601 02 Matlab Intro
16/57
Matrices
Rows and columns are always numbered starting at 1
Matlab matrices are of various types to hold differentkinds 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 needto be declared
Any matrix can be assigned to any variable
8/4/2019 Cis601 02 Matlab Intro
17/57
Matrices
Building matrices with [ ]:
A = [2 7 4]
A = [2; 7; 4]
A = [2 7 4; 3 8 9]
B = [ A A ]
2 7 4
2
7
4
2 7 43 8 9
?
8/4/2019 Cis601 02 Matlab Intro
18/57
Matrices
Building matrices with [ ]:
A = [2 7 4]
A = [2; 7; 4]
A = [2 7 4; 3 8 9]
B = [ A A ]
2 7 4
2
7
4
2 7 43 8 9
2 7 4
3 8 9
2 7 4
3 8 9
8/4/2019 Cis601 02 Matlab Intro
19/57
Matrices
8/4/2019 Cis601 02 Matlab Intro
20/57
Matrices
Some operators must be handled with care:
A = [1 2 ; 4 5]
B = A * A prints 9 1224 33
B = A .* A prints 1 4
16 25
Element by element multiplication
S b i
8/4/2019 Cis601 02 Matlab Intro
21/57
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
S b t i
8/4/2019 Cis601 02 Matlab Intro
22/57
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 a2 x 2 submatrix.
The rows and columns of the submatrix arerenumbered.
l
8/4/2019 Cis601 02 Matlab Intro
23/57
loops
for loops in MATLAB iterate over matrix elements:
b = 0for i = [ 3 9 17]
b = b + i;end
Result: 29
Note:THIS IS NOT THE WAY YOU SHOULD
PROGRAM A SUM, SINCE LOOPS ARE SLOWIN MATLAB !
loops
8/4/2019 Cis601 02 Matlab Intro
24/57
loops
for loops in MATLAB iterate over matrix elements:
b = 0for 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 !
loops
8/4/2019 Cis601 02 Matlab Intro
25/57
% 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
8/4/2019 Cis601 02 Matlab Intro
26/57
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
8/4/2019 Cis601 02 Matlab Intro
27/57
loops
Or a bit more sophisticated:
for i = 1:2:7
end
Which is the same as:
for i = [1 3 5 7]
end
loops
8/4/2019 Cis601 02 Matlab Intro
28/57
loops
Once again:
AVOID LOOPS
Avoiding loops
8/4/2019 Cis601 02 Matlab Intro
29/57
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
Avoiding loops
8/4/2019 Cis601 02 Matlab Intro
30/57
Thenon-MATLAB
way:
counter=0
For rows=1:2
For columns=1:6if M(rows,columns) > 5
inc counter
end
end
Avoiding loops
Avoiding loops
8/4/2019 Cis601 02 Matlab Intro
31/57
TheMATLAB
way:
T = M > 5;
Count = sum (T(:)) ;
Or shorter:
Count = sum(M(:) > 5);
g p
Avoiding loops
8/4/2019 Cis601 02 Matlab Intro
32/57
Another example, showing the mighty
INDEXING possibilities:
Task: eliminate the whitespaces in thefollowing vector M
g p
A B C D A B C D
Avoiding loops
8/4/2019 Cis601 02 Matlab Intro
33/57
i = find (M ~= ); % result: i=[1 3 4 6]M = M(i);
Or shorter:
M=M(find(M ~= );
g p
A B C D A B C D
Avoiding loops
8/4/2019 Cis601 02 Matlab Intro
34/57
survivingSubSegs = pointDensity >= minDensity;
diffSubSegs = diff([0 survivingSubSegs 0]);
startSeg = find(diffSubSegs == 1);
endSeg = find(diffSubSegs == -1) - 1;
An example how to work without loops,taken from the EMSF algorithm for line detection
Whats the purpose ?
Images
8/4/2019 Cis601 02 Matlab Intro
35/57
So why MATLAB and IMAGEPROCESSING ?
Images
8/4/2019 Cis601 02 Matlab Intro
36/57
Images can be treated as
matrices !
Images
8/4/2019 Cis601 02 Matlab Intro
37/57
Loading an image:
a = imread(picture.jpg);imshow(a);
Images
8/4/2019 Cis601 02 Matlab Intro
38/57
Image (=matrix) size:
size(a): 384 512 3
R G B
384
512
Images
8/4/2019 Cis601 02 Matlab Intro
39/57
Color image:
3D Matrix of RGB planes
Images
8/4/2019 Cis601 02 Matlab Intro
40/57
Show RED plane:
a(:,:,2:3) = 0;imshow(a);
Images
8/4/2019 Cis601 02 Matlab Intro
41/57
Show GREEN plane:
a(:,:,[1 3]) = 0;imshow(a);
Images
8/4/2019 Cis601 02 Matlab Intro
42/57
Show BLUE plane:
a(:,:,1:2) = 0;imshow(a);
Images
8/4/2019 Cis601 02 Matlab Intro
43/57
Advanced: Shuffling columns
rn = rand(1,512);
[rn1,i] = sort(rn);b = a(:,i,:);
imshow(b);
Images
8/4/2019 Cis601 02 Matlab Intro
44/57
By the way
8/4/2019 Cis601 02 Matlab Intro
45/57
MATLAB can also handle
Movies
3D objects
Conclusion
8/4/2019 Cis601 02 Matlab Intro
46/57
MATLAB is a mighty tool tomanipulate matrices
Images can be treated asmatrices
MATLAB is a mighty tool tomanipulate images
In my opinion
8/4/2019 Cis601 02 Matlab Intro
47/57
MATLAB should be used to code
software prototypes
Research is mostly aboutprototypes, not runtime-optimizedsoftware
MATLAB should be used inresearch
In my opinion
8/4/2019 Cis601 02 Matlab Intro
48/57
MATLAB prototypes must be re-coded (e.g. in C++) if theres need
for speed
Algorithm development time is
drastically shorter in MATLAB
Conclusion
8/4/2019 Cis601 02 Matlab Intro
49/57
CONCLUSION:
Use MATLAB
How to go on
8/4/2019 Cis601 02 Matlab Intro
50/57
MATLAB is exceptionally welldocumented:
Openinghelp/MATLAB help/Getting started
help/MATLAB help/Using MATLAB
Offers help to the environment,programming concepts, function
libraries etc How to go on
8/4/2019 Cis601 02 Matlab Intro
51/57
The help
command shows the first comment-lines of the function given by the m-
file .
Doc does the same, but nicer
(html-style, graphics, examples)
How to go on
8/4/2019 Cis601 02 Matlab Intro
52/57
The TAB key automatically extends commands
given. This is sometimes helpful if the functionname should be obvious:
Task: sort the rows of the matrix AIdea: there could be a function named sortXXXXType: sort + TABResult: sort,sortcellchar,sorted,sortiv,sortrows, sortrowsc
How to go on
8/4/2019 Cis601 02 Matlab Intro
53/57
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 charmatrix containing strings (+usage, examples etc.)
How to go on
8/4/2019 Cis601 02 Matlab Intro
54/57
Another helpful command is lookfor:
lookfor looks for the string in all
first comments lines (the ones shown by thehelp command) of ALL functions, returning
functions with a hit.
e.g. lookfor sort:
How to go on
8/4/2019 Cis601 02 Matlab Intro
55/57
e.g. lookfor sort:
bestof.m: %BESTOF(index,names,M,n,) returns the sorted comparison valuesof polygon(index) + filenamesCPLXPAIR 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
8/4/2019 Cis601 02 Matlab Intro
56/57
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 oftime-saving program collections.
How to go on
8/4/2019 Cis601 02 Matlab Intro
57/57
Now: browse the documentation !
read sample codes !
simply browse the list of functions(doc) Try and type some lines, use the
graphics !
top related