Introduction to MATLAB - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Introduction to MATLAB

Description:

... Useful Functions, Debugging, and Program Organization. Some ... nchoosek, randperm, rand, randn. Help. help, lookfor. Program organization -- Subfunctions ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 14
Provided by: hnlBc
Category:

less

Transcript and Presenter's Notes

Title: Introduction to MATLAB


1
Introduction to MATLAB
Lecture 4 Useful Functions, Debugging, and
Program Organization
  • Damon Tomlin
  • February 25, 2007

2
Some useful functions
  • Initializing variables
  • ones, zeros
  • Modifying variables
  • repmat, reshape
  • Statistics
  • max, min, mean, std, corrcoef, polyfit, ttest
  • Probability
  • nchoosek, randperm, rand, randn
  • Help
  • help, lookfor

3
Program organization -- Subfunctions
  • visible only to the main function
  • When should I use them?
  • Repeated calculations
  • Unique to the current program
  • Variable management

4
What is debugging?
  • A bug is an error in a program
  • Bugs come in two flavors
  • Those that keep a program from running
  • Those that dont
  • The first kind is relatively easy because of
    error messages

5
How do we debug a program?
  • keyboard (dbquit to return to main workspace)
  • dbup dbdown
  • profile
  • warnings and errors color coded on the right side
    (newer versions)

6
Some useful debugging tools
  • disp check on variables
  • breakpoints stop a program while it runs
  • keyboard the command form of breakpoints
  • try catch when an error occurs only sometimes

7
Program organization
  • Useful variable names
  • Nesting
  • Comments

8
Some examples
  • b 10
  • a 1 1
  • c 2 2
  • for i 31b
  • a(i) sum(a(i-2i-1))
  • c(i) sum(c(i-2i-1))
  • end

9
Some examples
  • NumElements 10
  • Start1 1 1 sequence starts with ones
  • Start2 2 2 sequence starts with twos
  • for i 31NumElements
  • Start1(i) sum(Start1(i-2i-1)) element is
    the sum of previous two elements
  • Start2(i) sum(Start2(i-2i-1))
  • end

10
More on comments
  • You can also use commenting to keep parts of your
    program from executing

11
Recursion when a program calls itself
  • Searches of unknown depth
  • function FoundIt Search(Target)
  • search file names for target
  • if no target is found
  • for each subdirectory
  • cd(subdirectory)
  • FoundIt Search(Target)
  • end
  • end
  • cd ..

12
Vectorization
  • Many loops can be vectorized
  • find, repmat, reshape, and element operations
    (. , ./, .) are useful for this
  • Matrix1 rand(100) Matrix1 rand(100)
  • Matrix2 rand(100) Matrix2 rand(100)
  • for i 11size(Matrix1,1) Matrix1(find(Matrix1
    lt Matrix2)) 0
  • for j 11size(Matrix1,2)
  • if Matrix1(i,j) lt Matrix2(i,j)
  • Matrix1(i,j) 0
  • end
  • end
  • end

13
Summary
  • MATLAB has built-in functions that help with
    analysis
  • Debugging
  • Program organization
Write a Comment
User Comments (0)
About PowerShow.com