Programming Basics for Logistics Algorithms: Lecture 2
1
Course Overview�Lecture 1
2
Course Overview�Lecture 2
3
Flashback: Lecture 1
4
5
And finally….
What is Matlab?
Matlab is both a program and a programming language ...
… it has lots of tools you can just use to analyse data and images. But you can also write code to extend it to do any analysis you like (although unlike a ‘pure’ language, it will only work if the Matlab program is installed on the computer).
Excel
Photoshop
Writing your own code
Using other people’s code
+
+
+
=
A really powerful tool for work and research!
6
What is a computer program?
Image(s)
Lab results
Patient data
DATA
Do something clever…
PROGRAM
Hopefully clinically useful!
RESULT
Example: in medical imaging…
Variable
7
Matlab Desktop
8
Variables
Command Window
Scripts
9
Press + to create new m-file in the matlab editor
Press Run button to run the script
10
Input/Output
>> a = input (‘What is your name? >’)
Expects user’s response and assigns the expression as a value to the variable a
Exercise:
Add to your helloWorld.m script the following lines:
>> myname=input('What is your name?>', 's’);
>> disp ('Written by ')
>> disp (myname)
What is your name?> Olof
Written by
Olof
11
Input/Output
Formatted output can be printed to the screen using the fprintf function (all in one line! unlike disp)
>> fprintf('The value is %d, for sure!', 4^3)
The value is 64, for sure!
Conversion character:
%d integer (it stands for decimal integer)
%f float (real number)
%c single character
%s string
integer number
place holder
for integer number
12
Input/Output
\n - provides printing from new line
>> fprintf('The value is %d, \n for sure!', 4^3)
The value is 64,
for sure! >>
More escape characters:
'' Single quotation mark %% Percent character
\t Horizontal tab \\ Backslash
new line
Questions?
Next: Algorithms
13
Algorithms
What is algorithm? (watch video 5 min)
or simply:
Pseudocode
- a simplified programming language, used in program design
14
Example: Area of a Circle
Algorithm (pseudocode):
15
Matlab code:
r = input ('Radius of the circle r = ');
A = pi*r^2;
disp ('Area of the circle A=')
disp (A)
Algorithm
16
Input
Output
Calculations
A typical sequential structure. Can be more complicated inside…
Algorithm
17
Questions?
Next: Flow Control
18
if/else/elseif
19
IF
if cond
commands end
ELSE
if cond
commands1 else
commands2 end
ELSEIF
if cond1
commands1 elseif cond2
commands2 else
commands3 end
Conditional statement: evaluates to true or false
Example: Area of a Circle
20
ATTN:
r>=0!
Relational Operators
21
| == | |
| ~= | |
| | > |
| < | |
short-circuit (scalars)
&
|
&&
||
~
Xor all any
Logical ‘or’ = only one is true
Conditional Statement
22
any non-zero value!
>> ANS: True
Conditional Statement
23
any non-zero value!
>> ANS: True
>> ANS: False
Conditional Statement
24
any non-zero value!
>> ANS: True
>> ANS: False
>> ANS: ALWAYS YES!!
WHY??
Conditional Statement
25
any non-zero value!
>> ANS: True
>> ANS: False
Nested If-else
26
Example: continuous mathematical function y(x):
Nested If-else
27
Example: continuous mathematical function y(x):
NOT efficient:
Better way:
Elseif
28
Example: continuous mathematical function y(x):
One more way: even more efficient
Note: only one ‘ end ‘ needed
ELSEIF
if cond1
commands1 elseif cond2
commands2 else
commands3 end
Switch
29
SWITCH
switch switch_expression
case case_expression 1
commands 1
case case_expression 2
commands 2
case case_expression 3
commands 3
... can be many of cases
otherwise (optional)
commands
end
- expression: scalar or string
- case_expressions are values
- one case at a time
when no cases match
Switch
30
Example: assign a letter grade corresponding to the integer number of points
Another way:
Using IF:
Menu
31
Example of Switch function usage
Questions?
Next: Loops
32
for
33
for
n=1:100
commands
end
Loop variable
for i = range commands
end
for
34
Print ‘Hello world’ 10 times with numbering the lines, each line in new row
>>
Hello world 1
Hello world 2
Hello world 3
Hello world 4
Hello world 5
Hello world 6
Hello world 7
Hello world 8
Hello world 9
Hello world 10
for
35
Print ‘Hello world’ 5 times with numbering the lines, for odd numbers only, starting from 1, each line in new row
>>
Hello world 1
Hello world 3
Hello world 5
Hello world 7
Hello world 9
step = 2
Nested for loops
Nested for loops
37
i = 1
j = 1
j = 2
i = 2
j = 1 %repeat
j = 2
i = 3
j = 1 %repeat
j = 2
inner loop
outer loop
while
38
WHILE
while cond commands
end
39
>>
i =
4
i =
16
i =
256
while
Step by step:
before the loop: i = 2
Iteration 1: check (2<100)
i = 2*2= 4
Iteration 2: check (4<100)
i = 4*4 = 16
Iteration 3: check (16<100)
i = 16*16 = 256
Iteration 4?: check (256>100)
exit
Next: Arrays
40
Questions?
Preliminaries: Vectors and Matrices
41
42
… I want to do something fun with images!
Those labs are dull…
What does an image look like to a computer?
43
A computer ‘sees’ an image as an array of numbers…
…. the value of each pixel represents its intensity (or colour)
We call each number a pixel (or in 3D a voxel)…
44
… I need to understand how to use arrays
So to learn how to do stuff with images…
Arrays
45
1D Arrays: vectors
46
●
●
●
1D Arrays: vectors
47
●
●
●
1D Arrays: vectors
48
●
○
○
○
1D Arrays: vectors
49
Automatic Initialization
50
)
(1,69
» o=ones(1,10)
» z=zeros(23,1)
» r=rand(1,45)
» n=nan
1D Arrays: vectors
51
More examples how to create vectors:
>> a = [1 2 3 4]
>> a = [1, 2, 3, 4]
>> a = 1:4 % no need for []
>> b = 1:2:9
b =
1 3 5 7 9
>> ? how to define the vector c:
9 7 5 3 1
1D Arrays: vectors
52
More examples how to create vectors:
>> a = [1 2 3 4]
>> a = [1, 2, 3, 4]
>> a = 1:4 % no need for []
>> b = 1:2:9
b =
1 3 5 7 9
>> ? how to define the vector c:
9 7 5 3 1
answer:
>> c = 9:-2:1
1D Arrays: vectors
53
linspace (x, y, n) function: creates a vector with n values in the inclusive range from x to y
>> v = linspace(3, 15, 5)
v = 3 6 9 12 15
logspace (x, y, n) function: creates a logarithmically spaced vector with n values in the inclusive range from 10^x to 10^y
>> logv = logspace(1, 5, 5)
logv =10 100 1000 10000 10000
Create vector using variables (Concatenation)
>> a = [1 10 100];
>> b = [2 3 4 5];
>> c = [a b]
c = 1 10 100 2 3 4 5
1D Arrays: Indexing and
accessing elements
54
Indexing vectors: starting from “1”
>> newvector=[1 3 5 7 9 3 6 9 12 15]
Accessing elements of the vector: vector(index) or vector(index vector)
>> newvector(3)
ans = 5
>> newvector(4:6)
ans = 7 9 3
>> newvector([1 10 5])
ans = 1 15 9 index vector
first element
1D Arrays: Changing values
55
Changing vector elements values
>> myvector=[1 3 5]
>> myvector (1) = 100;
>> myvector
myvector = 100 3 5
Adding new elements to the vector
>> myvector(4) = 11
>> myvector
myvector = 100 3 5 11
>> size (myvector)
ans = 1 4
new value
new element
Adding new elements to the vector
>> myvector(6) = 20
>> myvector
myvector = 100 3 5 11 0 20
>> size (myvector)
ans = 1 6
fills the gap
1D Arrays: Transpose
56
No way to use : operator directly
But can use transpose
>> myvector=1:2:5;
>> myvector
myvector = 1 3 5 7
>> myvector’
myvector =
1
3
5
7
Creating column vector
>> myvector=[1; 3; 5; 7];
>> myvector
myvector=
1
3
5
7
transpose
1D Arrays: Concatenation
57
Add elements to the empty vector
>> myvector=[];
>> myvector = [myvector 4]
myvector=
4� >> myvector = [myvector 2]
myvector =
4 2
Can be continued as many times as needed!
1D Arrays: Concatenation
58
Similarly we can create vectors of chars
>> myvector=['A', 'O', 'K', 'J'];
>> myvector = [myvector, 'B']
myvector=['A', 'O', 'K', 'J','B' ]
Next time: 2d Arrays=Matrices
59
Thank you!