7. First approach
IEEE upgrading
knowledge
Georgios
Drakopoulos
Code
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
function y = cipher1(x)
%CIPHER1 First approach to cipher system.
%
y = cipher1(x) transforms x into cipher y using
%
arithmetic operations.
if isa(x, 'char')
na = 'a'; nz = 'z';
y = na + mod(((x − na) + 3), nz − na + 1);
y = char(y);
else
error('Input is not a string.');
end
return
11. Second approach
IEEE upgrading
knowledge
Code
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
function y = cipher2(x)
%CIPHER2 Second approach to cipher system.
%
y = cipher2(x) transforms x into cipher y using tables.
Dynamic code
Final notes
T = 'a':'z';
%not proper but ok ..
if isa(x, 'char')
n = length(x);
for k = 1:n
xpos = find(x(k) == T);
ypos = circshift(xpos, 3);
y = [y T(ypos)];
end;
else
error('Input is not a string.');
end
return
14. find
function
IEEE upgrading
knowledge
Georgios
Drakopoulos
Syntax
MatLab central
Cipher system
Least squares
Dynamic code
% find non−zero entries of X by defult
I = find(X);
[I, J] = find(X);
Final notes
% find positive entries of X
I = find(X > 0)
% find the first n positive entries of X
I = find(X > 0, n);
Notes
Use sub2ind to switch from pairwise to linear.
Use ind2sub to switch from linear to pairwise.
15. Help functions
IEEE upgrading
knowledge
Georgios
Drakopoulos
help
MatLab central
Displays text based help.
Cipher system
Preamble comments are important.
Least squares
Dynamic code
Final notes
doc
Graphical help.
Extended function description.
lookfor
Term based search.
Similar to UNIX apropos command.
16. Wrapper function
IEEE upgrading
knowledge
Code
Georgios
Drakopoulos
MatLab central
Cipher system
function y = cipherw(x, method)
%CIPHERW Wrapper function to ciphper1 and cipher2.
Least squares
Dynamic code
Final notes
switch nargin
case 1
y = cipher2(x);
case 2
switch method
case 'numeric'
y = cipher1(x);
case 'table'
y = cipher2(x);
otherwise
error('Incorrect method.');
end
end
return
20. Least squares
The problem
IEEE upgrading
knowledge
Georgios
Drakopoulos
Data
MatLab central
Cipher system
Least squares
n observations in (xk , yk ) tabular format.
Linear relation between x and y .
Dynamic code
Final notes
yk = α0 xk + β0 ,
1 ≤ k ≤ n
Result
Compute α0 and β0 which minimize the cost function
n
(yk − (α0 xk + β0 ))
J (α0 , β0 ) =
k=1
2
21. Least squares
Solution
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Setting the derivative to zero yields
x1 1
x2 1
α0
. .
. . β0
. .
xn
1
A
x
y1
y2
= .
.
.
yn
b
Least squares solution
AT A ˆ = AT b
x
22. Least squares
The task
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Formulation
n
2
k=1 xk
n
k=1 xk
n
k=1 xk
n
α0
β0
Final notes
Data
Create the straight line y = 5x − 1
α0 = 5
β0 = −1
Corrupt it with noise.
Record performance vs noise power.
=
n
k=1 xk yk
n
k=1 yk
23. Sneak peek
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Code
Cipher system
Least squares
Dynamic code
Final notes
a0 = 5;
b0 = −1;
x = 0:15;
yc = a0*x + b0;
rng('shuffle');
Ey = norm(yc);
y = yc + sqrt(0.05*Ey)*randn(size(x));
save lsqtest a0 b0 x y
24. First approach
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Code
Cipher system
Least squares
Dynamic code
Final notes
function xls = lsq1(x, b)
%LSQ1 First approach to least squares.
%
xls = lsq1(x, b) solves normal equations.
T = [ norm(x)ˆ2 sum(x) ; sum(x) length(x) ];
g = [ dot(x,b) ; sum(b) ];
xls = inv(T) * g; %not always ok ...
return
25. Second approach
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Code
Least squares
Dynamic code
Final notes
function xls = lsq2(x, b)
%LSQ2 Second approach to least squares.
%
xls = lsq2(x, b) exploits slash operator.
T = [ x(:) ones(length(x), 1) ];
xls = T b(:);
return
26. Wrapper function
Control struct
IEEE upgrading
knowledge
Georgios
Drakopoulos
Code
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
load lsqtest
s = struct(
'x', x, ...
'b', y, ....
'method', 'slash' );
Notes
Older functions rely on nargin.
Newer functions rely on struct.
28. Wrapper function
Function
IEEE upgrading
knowledge
Georgios
Drakopoulos
Code
MatLab central
Cipher system
Least squares
Dynamic code
function xls = lsqw(s)
%LSQW Least squares wrapper function.
%
xls = lsqw(s)
Final notes
switch s.method
case 'slash'
xls = lsq2(s.x, s.b);
case 'normal'
xls = lsq1(s.x, s.b);
otherwise
warning('Incorrect method.');
end
return
29. Plot
Noisy data
IEEE upgrading
knowledge
Georgios
Drakopoulos
Code
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
xi = linspace(min(x), max(x), 10*length(x));
yi = interp1(x, y, xi, 'spline');
figure
hold on
plot(x, y, 'k'), plot(xi, yi, 'r'), plot(x, y, 'ro')
xlabel('Variable x')
ylabel('Variable y ')
legend('Linear', 'Cubic', 'Discrete', −1)
title('Least squares problem (linear and cubic data fit)')
print(gcf, '−depsc2', 'ieee2013 matlab2 lsq00.eps')
saveas(gcf, 'lsq00', 'fig')
hold off
30. Plot
Noisy data (figure)
IEEE upgrading
knowledge
Georgios
Drakopoulos
Least squares problem (linear and cubic data fit)
80
Linear
Cubic
Discrete
MatLab central
70
Cipher system
60
Least squares
Dynamic code
50
Variable y
Final notes
40
30
20
10
0
−10
0
5
10
Variable x
15
31. Plot
Projected data
IEEE upgrading
knowledge
Georgios
Drakopoulos
Code
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
yp = xls2(1) * x + xls2(2);
plot(x, yc, 'k', x, y, 'r', x, yp, 'b')
xlabel('Variable x')
ylabel('Variable y = alpha 0 x + beta 0')
title('Least squares problem ...
(argmin { | | y − A x | | 2ˆ2 })')
saveas(gcf, 'lsq01', 'fig')
print(gcf, '−depsc2', 'fig01.eps')
Notes
A
Limited LTEX support in title and x/ylabel.
32. Plot
Projected data (figure)
IEEE upgrading
knowledge
Georgios
Drakopoulos
Least squares problem (argmin { || y − A x||2 })
2
80
Original
Given
Projected
MatLab central
70
Cipher system
Least squares
60
Dynamic code
Variable y = α0 x + β0
50
Final notes
40
30
20
10
0
−10
0
5
10
Variable x
15
35. Another (hello) world
Function
IEEE upgrading
knowledge
Georgios
Drakopoulos
Code
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
function dyn(s)
%DYN Displays dynamic hello world.
%
%
dyn(s) displays "s: Hello world!"
z = [ 'disp( [ '' ' ...
char(s) ...
' '' '': Hello world!''] )' ];
disp(z);
eval(z);
return
36. Another (hello) world
Assessment
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Plus
Shorter code.
Flexible code.
Final notes
Easy to understand.
Minus
Overhead.
Difficult to understand.
38. Code acceleration
Overview
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes
Topics
Critical issue.
Sometimes even for proof of concept.
MatLab JIT compiler slow code compensation.
Vectorization.
Column-based operations.
MatLab uses column-major storage format.
Memory preallocation.
Known operand sizes.
39. Fast forward
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Topics
mex
connects C to MatLab.
matlab compiler
compiles MatLab code.
Dynamic code
Eclipse plugin for MatLab.
Final notes
A
Package mcode for LTEX.
Alternatives
Octave.
ScalaLab.
SciLab.
NumPy.
40. Final message
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
In brief
Join MatLab community.
Exploit MatLab flexibility.
Least squares
Be careful though.
Dynamic code
Final notes
Utilize help, doc, and lookfor.
There is more than one way to do things.
Wrapper functions.
save
intermediate data.
Optimize.
Have fun!
Programming is fun!