290 likes | 484 Views
Matlab Training Session 14: Improving Program Efficiency. Course Website: http://www.queensu.ca/neurosci/Matlab Training Sessions.htm. Course Outline Term 1 Introduction to Matlab and its Interface Fundamentals (Operators) Fundamentals (Flow) Importing Data Functions and M-Files
E N D
Matlab Training Session 14:Improving Program Efficiency • Course Website: • http://www.queensu.ca/neurosci/Matlab Training Sessions.htm
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 • GUI’s • Improving Code Efficiency
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
Part 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
Binary 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.2950e+009 • 64 bits 1.8447e+019! • Precision also determines how many decimal places can be represented
Numeric 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.
Readable 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
Part 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
Part B: Assigning Appropriate Data Resolution • Lets look at an example: testmatrix = magic(1000); testmatrix = single(testmatrix); [rowdim, coldim] = size(testmatrix); tic for rowloop = 1:rowdim for colloop = 1:coldim testmatrix(rowloop,colloop) = testmatrix(rowloop,colloop) * 50; end end toc
Part 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.
Part 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.
Part 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 = 1:rowdim for colloop = 1:coldim testmatrix(rowloop,colloop) = testmatrix(rowloop,colloop) + 50; end end toc
Part C: Assessing Execution Time • The tic, toc functions allow for the direct comparison of execution time when assessing the efficiency of different programming algorithms
Part 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!
Part C: Eliminating/Simplifying Loops For example, eliminate the for loop in the earlier example: testmatrix = magic(1000); [rowdim, coldim] = size(testmatrix); tic for rowloop = 1:rowdim for colloop = 1:coldim testmatrix(rowloop,colloop) = testmatrix(rowloop,colloop) + 50; end end toc
Part C: Eliminating/Simplifying Loops For example, eliminate the for loop in the earlier example: testmatrix = magic(1000); tic testmatrix(:,:) = testmatrix(:,:) + 50; toc
Part 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.
Part 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.
Part C: Eliminating/Simplifying Loops Slow looping method: [rowdim, coldim] = size(M); for i=1:rowdim, for j=1:coldim, if (M(i,j) > 4), M(i,j) = -M(i,j); end end end
Part C: Eliminating/Simplifying Loops Fast Vectorized Matlab Method: ind = find(M > 4); M(ind)=-M(ind);
Part 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 i=1:5, M(i,:)=M(i,:)-V(i); end
Part C: Eliminating/Simplifying Loops M = rand(10,5); V = mean(M); Fast Vectorized Matlab Method M=M-V(ones(10,1),:); That is, V is turned into a matrix the size of M and the matrices are subtracted in one operation.
Part 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)
Part 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 i=1:1000 for i=1:1000 • a=[17]; a=17; • end end • toc toc • elapsed_time = 0.1987 elapsed_time = 0.0435
Part 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 i=1:1000 for i=1:1000 • a=[17]; a=17; • end end • toc toc • elapsed_time = 0.1987 elapsed_time = 0.0435