IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code

IEEE upgrading know...
Agenda
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes

...
Agenda
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Topics
MatLab central

Cipher system
Least squares

C...
Overview
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code
Final notes...
Agenda
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Topics
MatLab central

Cipher system
Least squares

C...
Cipher system
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic code

Data
...
First approach
IEEE upgrading
knowledge
Georgios
Drakopoulos

Code

MatLab central
Cipher system
Least squares
Dynamic cod...
if

conditional

IEEE upgrading
knowledge
Georgios
Drakopoulos

Syntax

MatLab central
Cipher system
Least squares
Dynamic...
Class check
IEEE upgrading
knowledge
Georgios
Drakopoulos

Syntax

MatLab central
Cipher system

isa(x, 'char')

Least squ...
Assessment
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Plus
Simple.

Cipher system
Least squares

Quick....
Second approach
IEEE upgrading
knowledge

Code

Georgios
Drakopoulos
MatLab central
Cipher system
Least squares

function ...
for

loop

IEEE upgrading
knowledge
Georgios
Drakopoulos

Syntax

MatLab central
Cipher system
Least squares
Dynamic code
...
while

loop

IEEE upgrading
knowledge
Georgios
Drakopoulos

Syntax

MatLab central
Cipher system
Least squares
Dynamic cod...
find

function

IEEE upgrading
knowledge
Georgios
Drakopoulos

Syntax

MatLab central
Cipher system
Least squares
Dynamic ...
Help functions
IEEE upgrading
knowledge
Georgios
Drakopoulos

help
MatLab central

Displays text based help.

Cipher syste...
Wrapper function
IEEE upgrading
knowledge

Code

Georgios
Drakopoulos
MatLab central
Cipher system

function y = cipherw(x...
switch

conditional

IEEE upgrading
knowledge
Georgios
Drakopoulos

Syntax

MatLab central
Cipher system
Least squares
Dyn...
break

and

continue

IEEE upgrading
knowledge
Georgios
Drakopoulos

Syntax

MatLab central
Cipher system
Least squares
Dy...
Agenda
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Topics
MatLab central

Cipher system
Least squares

C...
Least squares
The problem

IEEE upgrading
knowledge
Georgios
Drakopoulos

Data
MatLab central
Cipher system
Least squares
...
Least squares
Solution

IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic c...
Least squares
The task

IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic c...
Sneak peek
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Code

Cipher system
Least squares
Dynamic code
Fi...
First approach
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Code

Cipher system
Least squares
Dynamic cod...
Second approach
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system

Code

Least squares
Dynamic co...
Wrapper function
Control struct

IEEE upgrading
knowledge
Georgios
Drakopoulos

Code
MatLab central
Cipher system
Least sq...
struct

handling

IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Functions
getfield

Cipher system

setfiel...
Wrapper function
Function

IEEE upgrading
knowledge
Georgios
Drakopoulos

Code

MatLab central
Cipher system
Least squares...
Plot
Noisy data

IEEE upgrading
knowledge
Georgios
Drakopoulos

Code

MatLab central
Cipher system
Least squares
Dynamic c...
Plot
Noisy data (figure)

IEEE upgrading
knowledge
Georgios
Drakopoulos

Least squares problem (linear and cubic data fit)
...
Plot
Projected data

IEEE upgrading
knowledge
Georgios
Drakopoulos

Code

MatLab central
Cipher system
Least squares
Dynam...
Plot
Projected data (figure)

IEEE upgrading
knowledge
Georgios
Drakopoulos

Least squares problem (argmin { || y − A x||2 ...
Graphics
IEEE upgrading
knowledge
Georgios
Drakopoulos

Handle
plot

MatLab central
Cipher system
Least squares
Dynamic co...
Agenda
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Topics
MatLab central

Cipher system
Least squares

C...
Another (hello) world
Function

IEEE upgrading
knowledge
Georgios
Drakopoulos

Code
MatLab central
Cipher system
Least squ...
Another (hello) world
Assessment

IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares...
Agenda
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central

Topics
MatLab central

Cipher system
Least squares

C...
Code acceleration
Overview

IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynam...
Fast forward
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares

Topics
mex

connect...
Final message
IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system

In brief
Join MatLab community.
...
Thank you!
Questions?

IEEE upgrading
knowledge
Georgios
Drakopoulos
MatLab central
Cipher system
Least squares
Dynamic co...
Upcoming SlideShare
Loading in …5
×

Ieee2013_upgrading_knowledge_matlab_pt2

328 views

Published on

IEEE Patras student branch - Upgrading knowledge (MATLAB workshop part 2)

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
328
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ieee2013_upgrading_knowledge_matlab_pt2

  1. 1. IEEE upgrading knowledge Georgios Drakopoulos MatLab central Cipher system Least squares Dynamic code IEEE upgrading knowledge MatLab workshop (second part) Final notes Georgios Drakopoulos CEID December 15, 2013
  2. 2. Agenda IEEE upgrading knowledge Georgios Drakopoulos MatLab central Cipher system Least squares Dynamic code Final notes Topics MatLab central Cipher system Least squares Dynamic code Final notes
  3. 3. Agenda IEEE upgrading knowledge Georgios Drakopoulos MatLab central Topics MatLab central Cipher system Least squares Cipher system Dynamic code Final notes Least squares Dynamic code Final notes
  4. 4. Overview IEEE upgrading knowledge Georgios Drakopoulos MatLab central Cipher system Least squares Dynamic code Final notes Official MatLab users community Questions can be posted and answered. Feedback required. Login credentials required; Free account.
  5. 5. Agenda IEEE upgrading knowledge Georgios Drakopoulos MatLab central Topics MatLab central Cipher system Least squares Cipher system Dynamic code Final notes Least squares Dynamic code Final notes
  6. 6. Cipher system IEEE upgrading knowledge Georgios Drakopoulos MatLab central Cipher system Least squares Dynamic code Data A lowercase letter string. Final notes Task A string where each original letter is shifted right by three. Wrap around ’z’.
  7. 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
  8. 8. if conditional IEEE upgrading knowledge Georgios Drakopoulos Syntax MatLab central Cipher system Least squares Dynamic code Final notes if x == 5 disp('five!'); elseif x == 6 disp('now six!'); else disp('something else!'); end Notes Equality with == Assignment with =
  9. 9. Class check IEEE upgrading knowledge Georgios Drakopoulos Syntax MatLab central Cipher system isa(x, 'char') Least squares Dynamic code Final notes Frequent classes single double logical (u)int8/16/32/64 cell struct function handle
  10. 10. Assessment IEEE upgrading knowledge Georgios Drakopoulos MatLab central Plus Simple. Cipher system Least squares Quick. Dynamic code Final notes Minus Assumes linear character representation. True for ASCII character set. Still machine dependent. Notes Frequently used by Julius Cæsar.
  11. 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
  12. 12. for loop IEEE upgrading knowledge Georgios Drakopoulos Syntax MatLab central Cipher system Least squares Dynamic code for k = a:s:b % do something with k end Final notes for k = a:b % assumes s = 1 end Notes a must be lower than b when s is positive. a must be greater than b when s is negative.
  13. 13. while loop IEEE upgrading knowledge Georgios Drakopoulos Syntax MatLab central Cipher system Least squares Dynamic code Final notes while a > b % do something end Notes for and while are equivalent. One form may be more convenient. Be careful when bounds change. Make sure loop terminates.
  14. 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. 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. 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
  17. 17. switch conditional IEEE upgrading knowledge Georgios Drakopoulos Syntax MatLab central Cipher system Least squares Dynamic code Final notes switch method case 'zero' disp('It''s zero point!'); case { 'one', 'two' } fprintf('%sn', 'A string'); case 4 pause(4) otherwise warning('Droplets in the ocean!'); end Notes cell groups strings and matrices.
  18. 18. break and continue IEEE upgrading knowledge Georgios Drakopoulos Syntax MatLab central Cipher system Least squares Dynamic code Final notes for k = 1:−0.005:1 if k ˜= 0 % do something else continue end end Notes break terminates innermost loop. continue jumps to next iteration. Same functionality with their C counterparts.
  19. 19. Agenda IEEE upgrading knowledge Georgios Drakopoulos MatLab central Topics MatLab central Cipher system Least squares Cipher system Dynamic code Final notes Least squares Dynamic code Final notes
  20. 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. 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. 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. 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. 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. 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. 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.
  27. 27. struct handling IEEE upgrading knowledge Georgios Drakopoulos MatLab central Functions getfield Cipher system setfield Least squares Dynamic code isfield Final notes rmfield fieldnames Notes z = struct('a', 1, 'b', 2); z = setfield(z, 'c', 3);
  28. 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. 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. 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. 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. 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
  33. 33. Graphics IEEE upgrading knowledge Georgios Drakopoulos Handle plot MatLab central Cipher system Least squares Dynamic code hold stem figure Final notes subplot xlabel title legend Print print saveas
  34. 34. Agenda IEEE upgrading knowledge Georgios Drakopoulos MatLab central Topics MatLab central Cipher system Least squares Cipher system Dynamic code Final notes Least squares Dynamic code Final notes
  35. 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. 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.
  37. 37. Agenda IEEE upgrading knowledge Georgios Drakopoulos MatLab central Topics MatLab central Cipher system Least squares Cipher system Dynamic code Final notes Least squares Dynamic code Final notes
  38. 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. 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. 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!
  41. 41. Thank you! Questions? IEEE upgrading knowledge Georgios Drakopoulos MatLab central Cipher system Least squares Dynamic code Final notes

×