Title: Matlab Training Session 14: Improving Program Efficiency
 1Matlab Training Session 14Improving Program 
Efficiency
Course Website http//www.queensu.ca/neurosci/Mat
lab Training Sessions.htm 
 2- Course Outline 
- Term 1 
- Introduction to Matlab and its Interface 
- Fundamentals (Operators) 
- Fundamentals (Flow) 
- Importing Data 
- Functions and M-Files 
- Plotting (2D and 3D) 
- Term 2 
- Term 1 review 
- Plotting (2D and 3D) 
- Statistical Tools in Matlab 
- Nonlinear Curve Fitting 
- Statistical Tools in Matlab II 
- GUIs 
- Improving Code Efficiency 
3- Week 14 Lecture Outline 
- Programming Efficiency 
- Data types in Matlab 
-  B. Assigning Appropriate Data Resolution 
- Downsampling from 64 bit data structures 
- B. Assessing Execution Time 
- tic, toc functions 
- Eliminating/Minimizing Loops 
- Compiling Code
4Part A Data types in Matlab 
 5Part A Data types in Matlab 
 6Part A Data types in Matlab 
 7Part A Assigning Appropriate Data Resolution
- By default, numeric values are saved in matlab as 
 64 bit floating point values
- Large data sets recorded experimentally generally 
 utilizes smaller or more specific data resolution
- Down-sampling large datasets can significantly 
 save on memory and dramatically improve execution
 time
8Binary Precision
-  The number of bits used to represent a value 
 determines how large or small that value can be
- 8 bits 0 to 256 
- 16 bits 0 to 65536 
- 32 bits 0 to 4.2950e009 
- 64 bits 1.8447e019! 
- Precision also determines how many decimal places 
 can be represented
9Numeric Formats Integers and Characters
'schar' Signed character 8 bits 'uchar' 
Unsigned character 8 bits 'int8' Integer 8 
bits 'int16' Integer 16 bits 'int32' 
Integer 32 bits 'int64' Integer 64 
bits 'uint8' Unsigned integer 8 bits 'uint16' 
 Unsigned integer 16 bits 'uint32' Unsigned 
integer 32 bits 'uint64' Unsigned integer 64 
bits 
 The first bit denotes the sign if the integer 
or character is signed. 
 10Readable Binary Data Formats Floating Point 
Representation
-  By default matlab stores all values with double 
 precision
-  The functions realmax and realmin return max and 
 min value representations
- 'float32, single Floating-point 32 bits 
- 'float64', 'double' Floating-point 64 bits
11Part B Assigning Appropriate Data Resolution
- The following functions can be used to convert 
 between numeric formats in matlab
-  single, double, uint8, uint16, int8, int16, 
 int32
-  
-  eg. single(x) will convert matrix x to 32 bit 
 IEEE floating point representation
- Operations on numeric formats are defined for 
 matlab version 7 and later
- BE CARFUL, EXCESSIVE DOWNSAMPLING CAN LEAD TO 
 ROUNDING ERRORS
12Part B Assigning Appropriate Data Resolution
- Lets look at an example 
- testmatrix  magic(1000) 
- testmatrix  single(testmatrix) 
- rowdim, coldim  size(testmatrix) 
- tic 
- for rowloop  1rowdim 
-  for colloop  1coldim 
-  testmatrix(rowloop,colloop)  
 testmatrix(rowloop,colloop)  50
-  end 
- end 
- toc 
13Part B Assessing Execution Time
- The tic and toc functions quantitatively assess 
 how long it takes to run any series of commands
 in Matlab
- tic starts a stopwatch timer. 
- toc prints the elapsed time since tic was used. 
14Part C Assessing Execution Time
- The tic and toc functions work together to 
 measure elapsed time. tic saves the current time
 that toc uses later to measure the elapsed time.
-  The sequence of commands 
- tic 
- operations 
- toc 
- measures the amount of time MATLAB takes to 
 complete one or more operations, and displays the
 time in seconds.
15Part C Assessing Execution Time
- For example, add 50 to every element of some 2D 
 matrix
- testmatrix  magic(1000) 
- rowdim, coldim  size(testmatrix) 
- tic 
- for rowloop  1rowdim 
-  for colloop  1coldim 
-  testmatrix(rowloop,colloop)  
 testmatrix(rowloop,colloop)  50
-  end 
- end 
- toc
16Part C Assessing Execution Time
- The tic, toc functions allow for the direct 
 comparison of execution time when assessing the
 efficiency of different programming algorithms
17Part C Eliminating/Simplifying Loops
- Since Matlab is an interpreted language, certain 
 common programming techniques (especially for
 loops) are intrinsically inefficient.
- Whenever possible, you should try to find a 
 vector function (or the composition of a few
 vector functions) that will accomplish the same
 result as a for loop.
- The process of converting a for loop to vector 
 function is referred to as vectorization
- The difference in processing time between 
 vectorization and a for loop can be astounding!
18Part C Eliminating/Simplifying Loops
- For example, eliminate the for loop in the 
 earlier example
- testmatrix  magic(1000) 
- rowdim, coldim  size(testmatrix) 
- tic 
- for rowloop  1rowdim 
-  for colloop  1coldim 
-  testmatrix(rowloop,colloop)  
 testmatrix(rowloop,colloop)  50
-  end 
- end 
- toc
19Part C Eliminating/Simplifying Loops
- For example, eliminate the for loop in the 
 earlier example
- testmatrix  magic(1000) 
- tic 
- testmatrix(,)  testmatrix(,)  50 
- toc
20Part C Eliminating/Simplifying Loops
- Lets look at some other common examples and then 
 list some common functions that aid in
 eliminating for loops through vectorization
- Two generic algorithms in matlab are 
- 1. Find a subset of a matrix that satisfies a 
 condition, and do something to it.
- 2. Do something different to each column of a 
 matrix without a loop.
21Part C Eliminating/Simplifying Loops
- Find a subset of a matrix that satisfies a 
 condition, and do something to it.
- Say you have a matrix 
- M  magic(3) 
- and you want to negate every element greater than 
 4.
22Part C Eliminating/Simplifying Loops
- Slow looping method 
- rowdim, coldim  size(M) 
- for i1rowdim, 
-  for j1coldim, 
-  if (M(i,j) gt 4), 
-  M(i,j)  -M(i,j) 
-  end 
-  end 
- end 
23Part C Eliminating/Simplifying Loops
- Fast Vectorized Matlab Method 
- ind  find(M gt 4) 
- M(ind)-M(ind) 
24Part C Eliminating/Simplifying Loops
- 2. Do something different to each column of a 
 matrix without a loop.
- In this case, let's subtract the mean from each 
 column of the matrix M
- M  rand(10,5) 
- V  mean(M) 
- Slow looping method 
- for i15, 
-  M(i,)M(i,)-V(i) 
- end 
25Part C Eliminating/Simplifying Loops
- M  rand(10,5) 
- V  mean(M) 
- Fast Vectorized Matlab Method 
- MM-V(ones(10,1),) 
- That is, V is turned into a matrix the size of M 
 and the matrices are subtracted in one operation.
26Part C Eliminating/Simplifying Loops
- Handy functions that aid in avoiding for loops 
 through vectorization are
- find (find values that meet some criteria) 
- sum, prod, diff (sum, product, difference) 
- . ./ (element by element matrix 
 operations)
- min, max (find min or max values) 
- zeros, ones (for initializing arrays) 
-  
27Part C Eliminating/Simplifying Loops
-  One other little tidbit Avoid using 's when 
 they aren't necessary.
-  MATLAB has to create temporary storage for the 
 results of what it believes to be concatenation.
- Eg 
- tic tic 
- for i11000 for i11000 
-  a17 a17 
- end end 
- toc toc 
- elapsed_time  0.1987 elapsed_time  0.0435 
28Part D Compiling Code
- MATLAB programs can be compiled has to create 
 temporary storage for the results of what it
 believes to be concatenation.
- Eg 
- tic tic 
- for i11000 for i11000 
-  a17 a17 
- end end 
- toc toc 
- elapsed_time  0.1987 elapsed_time  0.0435