Introduction to MATLAB - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Introduction to MATLAB

Description:

'find' is a function returning the indices of 1's. Where does this get us? Logical Operations ... What if there are stimuli after the scans are over? What if my ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 31
Provided by: hnlBc
Category:

less

Transcript and Presenter's Notes

Title: Introduction to MATLAB


1
Introduction to MATLAB
Lecture 1 the MATLAB environment, variables, and
flow control
  • Damon Tomlin
  • February 19, 2008

2
Why Program?
  • Fast
  • Error free calculations
  • Computers dont get bored

3
Whats a Program?
  • Set of instructions
  • Takes an input, creates an output

4
The MATLAB Environment
  • The workspace
  • The command line
  • Create new variables
  • Evaluate comparison
  • Execute functions
  • Check your variables
  • Alerts you to errors

5
Variables
  • Store values
  • Different types of data
  • Scalars (9, 3.14, 0, -1000000)
  • Vectors 1 2 3 4 5 6
  • Matrices 1 2 3 4 5 6
  • Strings Hello World!
  • Helpful name XCoord, Filename

6
Scripts
  • Scripts are programs .m
  • Scripts use the workspace
  • Scripts work with variables youve already created

7
Functions
  • Functions are programs .m
  • Functions create their own workspace
  • Functions take an input argument
  • Functions can produce an output
  • Variable inputs
  • MySum sum(a)
  • MySum sum(a,2)
  • New RotatePoint(orig, point, angle)

8
Operations
  • Mathematical statements
  • Add (), subtract (-), multiply(), divide(/)
  • Built-in functions exp(), log(), sqrt(), sin()
  • Order of operations 3(exp(log(2)) 2)

9
Expressions
  • Assignments
  • MyMatrix 1 2 3 4
  • MyName Damon
  • Comparisons
  • (MyAddress 1514)
  • (MyAge gt 20)

10
Manipulating Variables Indexing
  • An index is the location of a piece of data
    within a variable (like an address)
  • Indexing lets you manipulate a particular part of
    a variable
  • Vector(8)
  • Matrix(2,2)

5 1 2 3 4 8 9 5 1 2 3 4
5 1 2 3 4 8 9 5 1 2 3 4
11
Manipulating Variables Indexing
  • Indexing can be a little complex . . .
  • Vector(i)
  • y Vector(length(Vector))
  • x length(Vector)
  • Y Vector(x)
  • Multiple Dimensions Y(32,40,12,30)
  • Searching variables find
  • find(Vector 3)

12
Manipulating Variables Concatenating
  • Putting variables together
  • Z x y
  • Z x y
  • The size of the variable is important!
  • Often combined with indexing
  • Z(2,) y

13
Manipulating VariablesChanging Values
  • Creating a matrix
  • MyMatrix 1 2 3 4
  • When altering values, make sure the size of the
    output equals the size of the input
  • MyMatrix(1,2) 5
  • MyMatrix(,1) 1 1
  • Exception scalars
  • MyMatrix(,) 0

14
Manipulating VariablesMATLAB and matrices
  • The empty matrix
  • X(8)
  • Null results
  • Transpose X
  • Matrix vs. element operations
  • XY (rows in X columns in Y)
  • X.Y (X and Y are the same size)
  • Inf, NaN, pi, and i

5 1 2 3 4 8 9
5
1 2 3 4
5 9 5 1 1 2 2 3 3 4 4 8
5 1 2 3 4 8 9 5 1 2 3 4
p 8
15
Logical Operations
  • Comparison statements
  • (x 5)
  • (x gt 3)
  • (x 6)
  • Comparison statements return 1s and 0s
  • find is a function returning the indices of 1s
  • Where does this get us?

16
Logical Operations
  • Conditional statements tell a program what to do
    when certain events occur
  • What if a file isnt found?
  • What if there are stimuli after the scans are
    over?
  • What if my array has duplicated elements?
  • Contingencies can be stated as
  • IF condition 1 exists, perform instruction 1,
    OTHERWISE perform instruction 2

17
The if Statement
  • if (rem(x,2) 1)
  • disp(Odd)
  • end

18
The if Statement
  • if (rem(x,2) 1)
  • disp(Odd)
  • else
  • disp(Even)
  • end

19
The if Statement
  • if (rem(x,2) 1)
  • disp(Odd)
  • elseif (x 0)
  • disp(Zero)
  • else
  • disp(Even)
  • end

20
Combining Conditions
  • Sometimes execution depends on a coincidence of
    conditions
  • and if (rem(x,2) 0) (x gt 10)
  • or if (rem(x,2) 0) (x gt 10)
  • Combinations
  • if ((rem(x,2) 0) (x gt 10)) ((rem(x,2)
    1) (x lt 10))

21
Optimizing Conditionals
  • Sometimes we dont need to calculate every
    comparison
  • and as soon as one condition is false, the
    whole combination will return false
  • or as soon as one condition is true, the
    whole combination will return true
  • Short circuit and and or (newer
    versions only)

22
Loops
  • We often need to do a calculation multiple times
  • Registering multiple image files
  • Plotting multiple data sets for review
  • Iterating a simulation through time

23
for Loops
  • Perform instructions a set number of times
  • Have a counter variable
  • Good when the number of times needed to loop is
    easily calculable
  • Known of images

24
for Loops
  • for counter 014
  • X(counter1) 10counter
  • end
  • Answer X 1 10 100 1000 10000

25
while Loops
  • Perform instructions until a condition is met
  • Do not necessarily have a counter variable
  • Good when the number of times needed to loop is
    not easily calculable
  • Iterating a simulation as long as a criterion is
    met

26
while Loops
  • X 3
  • while (X(length(X)) lt 1000)
  • X(length(X)1) 3X(length(X))
  • end
  • Answer X 3 9 27 81 243 729 2187

27
Comparing for andwhile Loops
  • for counter 014
  • X(counter1) 10counter
  • end
  • counter 0
  • while (counter lt 4)
  • X(counter1) 10counter
  • counter counter 1
  • end
  • Initialization
  • Updating counters
  • Altering counters

28
switch statements -- a special conditional
  • Good for discrete numbers of possiblities (esp.
    strings)
  • Equivalent to a series of if/elseif/elses
  • switch(X)
  • case 1
  • disp(X equals 1)
  • case 2
  • disp(X equals 2)
  • otherwise
  • disp(Not in the list)
  • end

29
Summary
  • MATLAB uses programs to manipulate data through
    mathematical operations and functions
  • Data are stored in variables that can be indexed
  • Programs can manipulate data in different ways
    depending upon testable conditions
  • Programs can do repetitive calculations to save
    us time

30
Class Materials
  • www.hnl.bcm.edu
  • Courses/scm
  • Lectures, homeworks
Write a Comment
User Comments (0)
About PowerShow.com