Sie sind auf Seite 1von 25

LABORATORY WORK PREPARATION

Lab Work 1: Introduction to Matlab


Biomedical Informatics
Assoc. Prof. Tomaž Vrtovec, Ph.D.

University of Ljubljana, Faculty of Electrical Engineering Electrical Engineering, level 2


Laboratory of Imaging Technologies International course
Lab Work 1: Introduction to Matlab 2 / 25

MATLAB
Matrix laboratory

Matlab (matrix laboratory) is a software package for


numerical data analysis based on the 4th-generation
programming language. Year Version
1984 1.0
It enables: 1994 4.2c (R7): Windows operating system
- matrix manipulation 2000 6.0 (R12): Java support
- function and data visualization 2009 7.8 (R2009a): 32- in 64-bit version
- algorithm implementation 2014 8.4 (R2014b): Python support
- graphical user interface development 2017 9.2 (R2017a): Matlab Online

- linking other environments 2018 9.5 (R2018b)

(C, C++, Java, Fortran, Python) Extension Description


*.m Script, function, class
Toolboxes: *.fig Figure
- symbolic mathematics *.mat Binary data
- simulation *.mex Executable
- … *.p Protected contents
University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 3 / 25

MATLAB
Environment

Current folder

Variable Script Command


workspace editor window (>>)

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 4 / 25

MATLAB
Help

Description
of usage

Keyword search

Examples
of usage

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 5 / 25

MATLAB
Literature

- Matlab for Engineers


H. Moore, Prentice Hall, 3rd Edition, 2011
- An Engineer's guide to Matlab
E.B. Magrab et al., Prentice Hall, 2nd Edition, 2005
- Signals and Systems for Bioengineers
A MATLAB-based Introduction
J.L. Semmlow, Academic Press, 2nd Edition, 2012
- Numerical and Statistical Methods for Bioengineering
Applications in MATLAB
M.R. King & N.A. Mody, Cambridge University Press, 2011

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 6 / 25

VARIABLES  Element indexing starts with 1


(and not with 0 as e.g. in C).
Scalar and vector (row / column)

>> s = 10 >> v = [10, 20, 30] >> v = [10; 20; 30]

s = v = v =
10 10 20 30 10
20
>> ndims(s) 30

ans = >> v(2) >> v(3)


2
ans = ans =
>> size(s) 20 30

ans = >> v(1,2) >> v(3,1)


1 1
ans = ans =
20 30

>> v(2,1) >> v(1,3)


??? Index exceeds ??? Index exceeds
matrix dimensions. matrix dimensions.

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 7 / 25

VARIABLES
Matrix

>> M = [10, 20, 30; 40, 50, 60] >> M = [10, 20, 30; 40, 50]
??? Error using ==> vertcat
M = CAT arguments dimensions are not
10 20 30 consistent.
40 50 60
>> M = []
>> M(5)
M =
ans = []
30
>> isempty(M)
>> M(2,3)
ans =
ans = 1
60

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 8 / 25

VARIABLES
Multidimensional array

>> P(:,:,1) = [10, 20, 30; 40, ... >> P(2,3,1) >> P(end,2,end)
50, 60]
ans = ans =
P = 60 110
10 20 30
40 50 60 >> P(:,2:3,1) >> P(:)

>> P(:,:,2) = [70, 80, 90; 100, ... ans = ans =


110, 120] 20 30 10
50 60 40
P(:,:,1) = 20
10 20 30 >> P(:,[1,3],1) 50
40 50 60 30
ans = 60
P(:,:,2) = 10 30 70
70 80 90 40 60 100
100 110 120 80
110
90
120

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 9 / 25

VARIABLES
String

>> b = 'word'; >> b1 = 'biomedical';


>> b2 = 'informatics';
b =
word >> [b1, b2]

>> b(1) ans =


biomedicalinformatics
ans =
b >> [b1, ' ', b2]

>> b(2:3) ans =


biomedical informatics
ans =
or >> [upper(b1(1)), b1(2:end), ' ', b2]

ans =
Biomedical informatics

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 10 / 25

VARIABLES
Cell

>> c{1,1} = [1, 2, 3; 4, 5, 6]; >> c{1,1}

c = ans =
[2x3 double] 1 2 3
4 5 6
>> c{1,2} = [1, 2; 3, 4; 5, 6]
>> c{1,1}(:,2)
c =
[2x3 double] [3x2 double] ans =
2
>> c{2,1} = 'word' 5

c = >> c{2,2}
[2x3 double] [3x2 double]
'word‚ [] ans =
[]

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 11 / 25

OPERATIONS
Arithmetic operations

>> A = [1, 2; 3, 4]; B = A >> A + B >> A .^ B

B = ans = ans =
1 2 2 4 1 4
3 4 6 8 27 256

>> A * B >> A .* B >> A ^ -1

ans = ans = ans =


7 10 1 4 -2.0000 1.0000
15 22 9 16 1.5000 -0.5000

>> A / B >> A ./ B >> A .^ -1

ans = ans = ans =


1 0 1 1 1.0000 0.5000
0 1 1 1 0.3333 0.2500

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 12 / 25

OPERATIONS
Logical operations

>> 2 == 2 >> 'word' == 'word' >> 1 && 0

ans = ans = ans =


1 1 1 1 1 0

>> 2 ~= 2 >> 'word' == 'wrod' >> 1 || 0

ans = ans = ans =


0 1 0 0 1 1

>> 2 > 1 >> 'word' == 'wrd'


??? Error using ==> eq
ans = Matrix dimensions must agree.
1
>> strcmp('word', 'wrd')
>> 2 <= 1
ans =
ans = 0
0

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 13 / 25

OPERATIONS
Some useful operations on variables
Matrix size Steps Initial values
>> m = [1, 2, 3; ... >> v1 = 1:6 >> zeros(2, 3)
4, 5, 6];
>> size(m) v1 = ans =
1 2 3 4 5 6 0 0 0
ans = 0 0 0
2 3 >> v2 = 1:2:6
>> ones(2, 3)
>> length(m) v2 =
1 3 5 ans =
ans = 1 1 1
3 >> v3 = 1:3:6 1 1 1

Matrix transpose v3 = >> eye(3)


>> m = [1, 2, 3; ... 1 4
4, 5, 6]; ans =
>> m' >> v4 = 6:-3:1 1 0 0
0 1 0
ans = v4 = 0 0 1
1 4 6 3
2 5
3 6

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 14 / 25

OPERATIONS
Some useful operations on variables
Rounding Average, maximum, minimum Standard deviation
>> pi >> m = [1, 2, 3; ... >> std(m)
4, 5, 6]
ans = ans =
3.1416 m = 2.1213 2.1213 2.1213
1 2 3
>> round(pi) 4 5 6 >> std(std(m))

ans = >> mean(m) ans =


3 0
ans =
>> floor(pi) 2.5000 3.5000 4.5000 >> std([1, 2, 3, 4, 5, 6])

ans = >> mean(mean(m)) ans =


3 1.8708
ans =
>> ceil(pi) 3.5000 >> std(m(:))

ans = >> max(m); min(m); ans =


4 1.8708
>> sum(m); prod(m);

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 15 / 25

COMMANDS
Iterative loops: for / while

>> s = 0; >> s = 0; >> s = 0;

>> for i = 0:4 >> for i = 0:2:4 >> while s < 10


s = s + i s = s + i s = s + 2
end end end

s = s = s =
0 0 2

s = s = s =
1 2 4

s = s = s =
3 6 6

s = s =
6 8

s = s =
10 10

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 16 / 25

COMMANDS
Conditionals: if / if-else / if-elseif-else / switch-case-otherwise

>> s1 = 10; s2 = 20; >> s1 = 10; s2 = 20; >> z = 2;

>> if s1 ~= s2 >> if s1 == s2 >> switch z


disp('NOT EQUAL'); disp('EQUAL'); case 1
end elseif s1 < s2 disp('ONE');
disp('s1 LESSER'); case 2
NOT EQUAL else disp('TWO');
disp('s1 GREATER'); case {3, 4}
>> if s1 == s2 end disp('THREE/FOUR');
disp('EQUAL'); otherwise
else S1 LESSER disp('UNKNOWN');
disp('NOT EQUAL'); end
end
TWO
NOT EQUAL

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 17 / 25

COMMANDS  The script (or function) filename must


not be equal to the reserved words.
Command nesting and script example
Script – a file named “MyProgram.m”
% clear memory and wipe the command window >> MyProgram
clear all; clc;
20/10/10: TWO EQUAL
% example of command nesting 20/10/20: TWO EQUAL
s1 = 20; 20/10/30: ALL DIFFERENT
for s2 = 10:10:30 20/20/10: TWO EQUAL
for s3 = [10, 20, 30] 20/20/20: ALL EQUAL
b = [num2str(s1),'/', ... 20/20/30: TWO EQUAL
num2str(s2),'/', ... 20/30/10: ALL DIFFERENT
num2str(s3)]; 20/30/20: TWO EQUAL
if s1 == s2 && s1 == s3 20/30/30: TWO EQUAL
disp([b, ': ALL EQUAL']);
elseif s1 == s2 || s1 == s3 || s2 == s3
disp([b, ': TWO EQUAL']);
else
disp([b, ': ALL DIFFERENT']);
end
end
end

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 18 / 25

FUNCTIONS  Function filename must be equal to the


declaration of the function.
Declaration and call
Function – a file named “SandP.m”
function [summa, product] = SandP(value1, value2, value3)

summa = value1 + value2 + value3;


product = value1 * value2 * value3;

>> SandP(3,4,5) >> SandP(3,4)


??? Input argument "value3" is undefined.
ans =
12 Error in ==> SandP at 3
summa = value1 + value2 + value3;
>> [s, p] = SandP(3, 4, 5)
s = >> [s, p, r] = SandP(3, 4, 5)
12 ??? Error using ==> SandP
Too many output arguments.
p =
60

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 19 / 25

FUNCTIONS  Use nargout for the default


values of output arguments.
Default values of input arguments
Function – a file named “Summa.m”
function summa = Summa(value1, value2, value3) >> Summa(3,4,5)

if nargin < 3 ans =


value3 = 0; 12
end
if nargin < 2 >> Summa(3,4)
value2 = 0;
end ans =
if nargin < 1 7
value1 = 0;
end >> Summa(3)
summa = value1 + value2 + value3;
ans =
3

>> Summa()

ans =
0

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 20 / 25

FUNCTIONS  Use varargout for a variable


number of output arguments.
Variable number of input arguments
Function – a file named “Compute.m”
function R = Compute(op, varargin) >> Compute('sum', 1, 2, 3, 4, 5)

if strcmp(op, 'sum') ans =


R = 0; 15
for i = 1:length(varargin)
R = R + varargin{i}; >> Compute('product', 1, 2, 3, 4)
end
elseif strcmp(op, 'product') ans =
R = 1; 24
for i = 1:length(varargin)
R = R * varargin{i}; >> Compute('difference', 1, 2)
end
else Operation "difference" is unknown.
disp(['Operation "', ...
op, '" is unknown.']); >> [sum(1:5), prod(1:4)]
end
ans =
15 24

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 21 / 25

VISUALIZATION  Add graphs to the same coordinate


system by using hold on.
Graph plotting

>> x = -pi:0.1:pi;
>> y1 = sin(x); y2 = cos(x);
Sine and cosine
>> plot(x, y1);
>> plot(x, y1, x, y2);
>> plot(x, y1, 'r-', x, y2, 'b:');
>> plot(x, y1, 'r-', x, y2, ...
'b:', 'LineWidth', 3);

>> axis([-pi, pi, -1, 1]);


>> legend('sin(x)', 'cos(x)');

>> title('Sine and cosine')


>> xlabel('x'); ylabel('y');

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 22 / 25

VISUALIZATION  The visualization of every component can be adjusted


through handles, accessed by get and set.
Customizing the plot

>> h = title('Sine and cosine')

h = Sine
Sine and cosine
Sine and
and cosine
cosine
182.0078

>> get(h)
...
FontSize = [10]
FontWeight = normal
...

>> set(h, 'FontSize', 12 , ...


'FontWeight', 'bold');

>> h = xlabel('x');
>> set(h, 'FontSize', 12 , ...
'FontWeight', 'bold');
>> h = ylabel('y');
>> set(h, 'FontSize', 12 , ...
'FontWeight', 'bold');

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 23 / 25

VISUALIZATION
Customizing the plot

>> get(gca)
... Sine and cosine

FontSize = [10] Sine


Sineand
andcosine
cosine
...

>> set(gca, 'FontSize', 12);

>> get(gcf)
...
Color = [0.8 0.8 0.8]
...
Name =
...

>> set(gcf, 'Color', [1 1 1], ...


'Name', 'Graph in 2D');

>> print(gcf, '-djpeg', ...


'-r300', 'Graph2D.jpg')

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 24 / 25

LABORATORY WORK
Lab Work 1: Introduction to Matlab

Bubble sort is a simple number sorting algorithm:


- we pass through the numbers,
- when each following number is less than the current number, we
swap the position of these two numbers,
- we repeat the passes until the numbers are sorted.

4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1
2 2 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 1 1 2 2
5 5 5 5 5 5 3 3 3 3 3 3 4 4 1 1 1 1 3 3 3 3
3 3 3 3 3 3 5 5 1 1 1 1 1 1 4 4 4 4 4 4 4 4
1 1 1 1 1 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5
1st pass 2nd pass 3rd pass 4th pass
(4 ×) (3 ×) (2 ×) (1 ×)

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course
Lab Work 1: Introduction to Matlab 25 / 25

LABORATORY WORK
Lab Work 1: Introduction to Matlab

Bubble sort Bubble sort


Element position

Element position

Element value Element value

University of Ljubljana, Faculty of Electrical Engineering BIOMEDICAL INFORMATICS Electrical Engineering, level 2
Laboratory of Imaging Technologies Assoc. Prof. Tomaž Vrtovec, Ph.D. International course

Das könnte Ihnen auch gefallen