Lecture 6 - 10/12/2010 Lecture 6: Script Design, Algorithm...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
10/12/2010 1 Lecture 6: Script Design, Algorithm Development , Debugging BMEn 2401 Programming for Biomedical Engineers Rich Beck October 12, 2010 This Lecture Analyzing Algorithms Speed tic toc Vectorization vs loops Preallocation Linear indexing vs subscript indexing EXAMPLE Profiler Debugging M-LINT Code checker Other techniques 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10/12/2010 2 Analyzing Algorithms Choosing between multiple methods of implementing an algorithm depends on these and other factors: The accuracy and precision of the result The amount of time the computation takes The amount of memory space needed for the computation 3 Speed: tic and toc Tool for timing MATLAB code execution Place the command tic before the line(s) of code which you wish to time Place the command toc after the line(s) of code which you wish to time Output Elapsed time in seconds 4
Background image of page 2
10/12/2010 3 Creating Arrays in Loops Example: for j=1:4 v(j) = j; end disp(v) Slowest Method for Array Creation/Change 1. When j=1 1. Assignment v=1 (1x1 scalar) 2. Add Increment/spacing 1 3. When j=2 1. First v=1 is copied into a new 1x2 v array where v(1)=1 2. Then 1x1 scalar v erased 3. Then v(2)=2 4. Add Increment/spacing 1 5. When j=3 1. First v = 1 2 is copied into a new 1x3 v array where v(1)=1, v(2)=2 2. Then 1x2 v erased 3. Then v(3)=3 5 . . . Speed: Vectorization Element-by-element operations tic v=1:4; disp(v) toc 6 When speed is important, always look for ways to vectorize your algorithms Fastest Method for Array Creation/Change 1. Assignment v(1)=1 2. Add Increment/spacing 1 3. Assignment v(2)=2 4. Add Increment/spacing 1 5. Assignment v(3)=3 6. Add Increment/spacing 1 7. Assignment v(4)=4
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10/12/2010 4 Vectorization Example Element-by-element operations tic x=0:0.1:10; y=log10(x); toc Loops tic x=0; for k=1:1001 y(k)=log10(x); x=x+.01; end toc 7 If you cannot vectorize your code, preallocating vectors or matrices speeds up loop execution and manages
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

Lecture 6 - 10/12/2010 Lecture 6: Script Design, Algorithm...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online