Matlab Training Session 14: Improving Program Efficiency - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Matlab Training Session 14: Improving Program Efficiency

Description:

Introduction to Matlab and its Interface. Fundamentals (Operators) Fundamentals (Flow) ... One other little tidbit: Avoid using []'s when they aren't necessary. ... – PowerPoint PPT presentation

Number of Views:186
Avg rating:3.0/5.0
Slides: 29
Provided by: Rob154
Category:

less

Transcript and Presenter's Notes

Title: Matlab Training Session 14: Improving Program Efficiency


1
Matlab 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

4
Part A Data types in Matlab
5
Part A Data types in Matlab
6
Part A Data types in Matlab
7
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

8
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.2950e009
  • 64 bits 1.8447e019!
  • Precision also determines how many decimal places
    can be represented

9
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.
10
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

11
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

12
Part 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

13
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.

14
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.

15
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 1rowdim
  • for colloop 1coldim
  • testmatrix(rowloop,colloop)
    testmatrix(rowloop,colloop) 50
  • end
  • end
  • toc

16
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

17
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!

18
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 1rowdim
  • for colloop 1coldim
  • testmatrix(rowloop,colloop)
    testmatrix(rowloop,colloop) 50
  • end
  • end
  • toc

19
Part C Eliminating/Simplifying Loops
  • For example, eliminate the for loop in the
    earlier example
  • testmatrix magic(1000)
  • tic
  • testmatrix(,) testmatrix(,) 50
  • toc

20
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.

21
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.

22
Part 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

23
Part C Eliminating/Simplifying Loops
  • Fast Vectorized Matlab Method
  • ind find(M gt 4)
  • M(ind)-M(ind)

24
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 i15,
  • M(i,)M(i,)-V(i)
  • end

25
Part 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.

26
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)

27
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 i11000 for i11000
  • a17 a17
  • end end
  • toc toc
  • elapsed_time 0.1987 elapsed_time 0.0435

28
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 i11000 for i11000
  • a17 a17
  • end end
  • toc toc
  • elapsed_time 0.1987 elapsed_time 0.0435
Write a Comment
User Comments (0)
About PowerShow.com