Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research

Description:

Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research Non-Accredited Matlab Tutorial Sessions for beginner to intermediate level users ... – PowerPoint PPT presentation

Number of Views:2151
Avg rating:3.0/5.0
Slides: 57
Provided by: brainb8
Category:

less

Transcript and Presenter's Notes

Title: Matlab Beginner Training Session Review: Introduction to Matlab for Graduate Research


1
Matlab Beginner Training Session
ReviewIntroduction to Matlab for Graduate
Research
2
  • Non-Accredited Matlab Tutorial Sessions for
    beginner to intermediate level users
  • Winter Session Dates  February 13, 2007 - March
    7, 2007Session times Tuesdays from
    830am-1000am, Wednesdays from
    830pm-1000amSession Locations Humphrey Hall -
    Room 219
  • Instructors
  • Robert Marino rmarino_at_biomed.queensu.ca
  • Course Website
  • http//www.queensu.ca/neurosci/Matlab Training
    Sessions.htm

3
  • Last Semester
  • Weeks
  • Introduction to Matlab and its Interface
  • Fundamentals (Operators)
  • Fundamentals (Flow)
  • Importing Data
  • Functions and M-Files
  • Plotting (2D and 3D)
  • Statistical Tools in Matlab
  • Analysis and Data Structures

4
Intermediate Sessions
  • Intermediate Lectures
  • Term 1 review
  • Loading Binary Data
  • Nonlinear Curve Fitting
  • Statistical Tools in Matlab II
  • Creating Graphic User Interfaces (GUIs)
  • Other possible topics
  • Writing ascii text data files
  • 3D plotting and animating
  • Debugging Tools
  • Simulink Toolbox


5
Why Matlab?
  • Common Uses for Matlab in Research
  • Data Acquisition
  • Multi-platform, Multi Format data importing
  • Analysis Tools (Existing,Custom)
  • Statistics
  • Graphing
  • Modeling

6
Why Matlab?
  • Multi-platform, Multi Format data importing
  • Data can be loaded into Matlab from almost any
    format and platform
  • Binary data files (eg. REX, PLEXON etc.)
  • Ascii Text (eg. Eyelink I, II)
  • Analog/Digital Data files

PC
100101010
UNIX
Subject 1 143 Subject 2 982 Subject 3 87
7
Why Matlab?
  • Analysis Tools
  • A Considerable library of analysis tools exist
    for data analysis
  • Provides a framework for the design, creation,
    and implementation of any custom analysis tool
    imaginable

8
Why Matlab?
  • Graphing
  • A Comprehensive array of plotting options
    available from 2 to 4 dimensions
  • Full control of formatting, axes, and other
    visual representational elements

9
Why Matlab?
  • Modeling
  • Models of complex dynamic system interactions can
    be designed to test experimental data

10
Understanding the Matlab Environment
  • Executing Commands
  • Basic Calculation Operators
  • Addition
  • - Subtraction
  • Multiplication
  • / Division
  • Exponentiation

11
Using Matlab
  • Solving equations using variables
  • Matlab is an expression language
  • Expressions typed by the user are interpreted and
    evaluated by the Matlab system
  • Variables are names used to store values
  • Variable names allow stored values to be
    retrieved for calculations or permanently saved
  • Variable Expression
  • Or
  • Expression
  • Variable Names are Case Sensitive!

gtgt x y Ans 12 gtgt x / y Ans 3 gtgt x y
Ans 36
gtgt x 6 x 6 gtgt y 2 y 2 gtgt x y Ans 8
12
Using Matlab
  • Working with Matrices
  • Matlab works with essentially only one kind of
    object, a rectangular numerical matrix
  • A matrix is a collection of numerical values
    that are organized into a specific configuration
    of rows and columns.
  • The number of rows and columns can be any number
  • Example
  • 3 rows and 4 columns define a 3 x 4 matrix
    having 12 elements
  • A scalar is a single number and is represented by
    a 1 x 1 matrix in matlab.
  • A vector is a one dimensional array of numbers
    and is represented by an n x 1 column vector or a
    1 x n row vector of n elements

13
Exercises
Enter the following Matrices in matlab using
spaces, commas, and semicolons to separate rows
and columns
A
B
D
D
C
E a 5 x 9 matrix of 1s
14
Exercises
Change the following elements in each matrix
76
76
0
A
B
0
D
76
0
D
C
76
E a 5 x 9 matrix of 1s
76
15
Matrix Operations
  • Indexing Matrices
  • A 1 2 4 5
  • 6 3 8 2
  • The colon operator can can be used to remove
    entire rows or columns
  • gtgt A(,3)
  • A 1 2 5
  • 6 3 2
  • gtgt A(2,)
  • A 1 2 5

16
Matrix Operations
  • Scalar Operations
  • Scalar (single value) calculations can be can
    performed on matrices and arrays
  • Basic Calculation Operators
  • Addition
  • - Subtraction
  • Multiplication
  • / Division
  • Exponentiation

17
Matrix Operations
  • Element by Element Multiplication
  • Element by element multiplication of matrices is
    performed with the . operator
  • Matrices must have identical dimensions
  • A 1 2 B 1 D 2 2 E 2
    4 3 6
  • 6 3 7 2 2
  • 3
  • 3
  • gtgtA . D
  • Ans 2 4
  • 12 6

18
Matrix Operations
  • Element by Element Division
  • Element by element division of matrices is
    performed with the ./ operator
  • Matrices must have identical dimensions
  • A 1 2 4 5 B 1 D 2 2 2 2 E
    2 4 3 6
  • 6 3 8 2 7 2
    2 2 2
  • 3
  • 3
  • gtgtA ./ D
  • Ans 0.5000 1.0000 2.0000 2.5000
  • 3.0000 1.5000 4.0000 1.0000

19
Matrix Operations
  • Matrix Exponents
  • Built in matrix Exponentiation in Matlab is
    either
  • A series of Algebraic dot products
  • Element by element exponentiation
  • Examples
  • A2 A A (Matrix must be square)
  • A.2 A . A

20
Matrix Operations
  • Shortcut Transposing Matrices
  • The transpose of a matrix is the matrix formed by
    interchanging the rows and columns of a given
    matrix
  • A 1 2 4 5 B 1
  • 6 3 8 2 7
  • 3
  • 3
  • gtgt transpose(A) gtgt
    B
  • A 1 6
    B 1 7 3 3
  • 2 3
  • 4 8
  • 5 2

21
Relational Operators
  • Relational operators are used to compare two
    scaler values or matrices of equal dimensions
  • Relational Operators
  • lt less than
  • lt less than or equal to
  • gt Greater than
  • gt Greater than or equal to
  • equal
  • not equal

22
Relational Operators
  • Comparison occurs between pairs of corresponding
    elements
  • A 1 or 0 is returned for each comparison
    indicating TRUE or FALSE
  • Matrix dimensions must be equal!
  • gtgt 5 5
  • Ans 1
  • gtgt 20 gt 15
  • Ans 1

23
Relational Operators
A 1 2 4 5 B 7 C 2 2 2 2
6 3 8 2 2 2 2
2 Try gtgtA gt B gtgt A lt C

24
Relational Operators
  • The Find Function
  • A 1 2 4 5 B 7 C 2 2 2 2
    D 0 2 0 5 0 2
  • 6 3 8 2 2
    2 2 2
  • The find function can also return the row and
    column indexes of of matching elements by
    specifying row and column arguments
  • gtgt x,y find(A 5)
  • The matching elements will be indexed by (x1,y1),
    (x2,y2),
  • gtgt A(x,y) 10
  • A 1 2 4 10
  • 6 3 8 2

25
Control and Flow
  • Control flow capability enables matlab to
    function beyond the level of a simple desk
    calculator
  • With control flow statements, matlab can be used
    as a complete high-level matrix language
  • Flow control in matlab is performed with
    condition statements and loops

26
Matlab Scripts
  • Advantages of M-files
  • Easy editing and saving of work
  • Undo changes
  • Readability/Portability - non executable comments
    can be added using the symbol to make make
    commands easier to understand
  • Saving M-files is far more memory efficient than
    saving a workspace

27
Condition Statements
  • It is often necessary to only perform matlab
    operations when certain conditions are met
  • Relational and Logical operators are used to
    define specific conditions
  • Simple flow control in matlab is performed with
    the If, Else, Elseif and Switch statements

28
Condition Statements
  • If, Else, and Elseif
  • An if statement evaluates a logical expression
    and evaluates a group of commands when the
    logical expression is true
  • The list of conditional commands are terminated
    by the end statement
  • If the logical expression is false, all the
    conditional commands are skipped
  • Execution of the script resumes after the end
    statement
  • Basic form
  • if logical_expression
  • commands
  • end

29
Condition Statements
Example A 6 B 0 if A gt 3
D 1 2 6 A A 1 elseif A gt 2 D
D 1 A A 2 end What is evaluated in
the code above?
30
Condition Statements
  • Switch
  • The switch statement can act as many elseif
    statements
  • Only the one case statement whos value satisfies
    the original expression is evaluated
  • Basic form
  • switch expression (scalar or string)
  • case value 1
  • commands 1
  • case value 2
  • commands 2
  • case value n
  • commands n
  • end

31
Condition Statements
Example A 6 B 0 switch
A case 4 D 0 0 0 A A - 1 case 5
B 1 case 6 D 1 2 6 A A
1 end Only case 6 is evaluated
32
Loops
  • Loops are an important component of flow control
    that enables matlab to repeat multiple statements
    in specific and controllable ways
  • Simple repetition in matlab is controlled by two
    types of loops
  • For loops
  • While loops

33
Loops
  • For Loops
  • The for loop executes a statement or group of
    statements a predetermined number of times
  • Basic Form
  • for index startincrementend
  • statements
  • end
  • If increment is not specified, an increment
    of 1 is assumed by matlab

34
Loops
  • For Loops
  • Examples
  • for i 11100
  • x(i) 0
  • end
  • Assigns 0 to the first 100 elements of vector x
  • If x does not exist or has fewer than 100
    elements, additional space will be automatically
    allocated

35
Loops
  • For Loops
  • Loops can be nested in other loops
  • A
  • for i 1m
  • for j 1n
  • A(i,j) i j
  • end
  • end
  • Creates an m by n matrix A whose elements are the
    sum of their matrix position

36
Loops
  • While Loops
  • The while loop executes a statement or group of
    statements repeatedly as long as the controlling
    expression is true
  • Basic Form
  • while expression
  • statements
  • end

37
Loops
  • While Loops
  • Examples
  • A 6 B 15
  • while A gt 0 B lt 10
  • A A 1
  • B B 2
  • end
  • Iteratively increase A and decrease B until the
    two conditions of the while loop are met
  • Be very careful to ensure that your while loop
    will eventually reach its termination condition
    to prevent an infinite loop

38
Loops
  • Breaking out of loops
  • The break command instantly terminates a for
    and while loop
  • When a break is encountered by matlab, execution
    of the script continues outside and after the
    loop

39
Loops
  • Breaking out of loops
  • Example
  • A 6 B 15
  • count 1
  • while A gt 0 B lt 10
  • A A 1
  • B B 2
  • count count 1
  • if count gt 100
  • break
  • end
  • end
  • Break out of the loop after 100 repetitions if
    the while condition has not been met

40
Functions in Matlab
  • In Matlab, each function is a .m file
  • It is good protocol to name your .m file the same
    as your function name, i.e. funcname.m
  • function outargsfuncname(inargs)

Function
input
output
41
Importing Data
  • Basic issue
  • How do we get data from other sources into Matlab
    so that we can play with it?
  • Other Issues
  • Where do we get the data?
  • What types of data can we import
  • Easily or Not

42
Basics
  • Matlab has a powerful plotting engine that can
    generate a wide variety of plots.

43
Generating Data
  • Matlab does not understand functions, it can only
    use arrays of numbers.
  • at2
  • bsin(2pit)
  • ce-10t note matlab command is exp()
  • dcos(4pit)
  • e2t3-4t2t
  • Generate it numerically over specific range
  • Try and generate a-e over the interval 00.012

t00.0110 make x vector yt.2 now we have
the appropriate y but only over the
specified range
44
Quick Assignment 1
  • Plot a as a thick black line
  • Plot b as a series of red circles.
  • Label each axis, add a title and a legend

45
Quick Assignment 1
figure plot(t,a,'k','LineWidth',3) hold
on plot(t,b,'ro') xlabel('Time
(ms)') ylabel('f(t)') legend('t2','sin(2pit)'
) title('Mini Assignment 1')
46
Part A Basics
  • The Matlab installation contains basic
    statistical tools.
  • Including, mean, median, standard deviation,
    error variance, and correlations
  • More advanced statistics are available from the
    statistics toolbox and include parametric and
    non-parametric comparisons, analysis of variance
    and curve fitting tools

47
Mean and Median
Mean Average or mean value of a
distribution Median Middle value of a sorted
distribution M mean(A), M median(A) M
mean(A,dim), M median(A,dim) M mean(A), M
median(A) Returns the mean or median value of
vector A. If A is a multidimensional mean/median
returns an array of mean values. Example A
0 2 5 7 20 B 1 2 3
3 3 6 4 6 8 4 7 7
mean(A) 6.8 mean(B) 3.0000 4.5000 6.0000
(column-wise mean) mean(B,2) 2.0000 4.0000
6.0000 6.0000 (row-wise mean)
48
Standard Deviation and Variance
  • Standard deviation is calculated using the std()
    function
  • std(X) Calcuate the standard deviation of
    vector x
  • If x is a matrix, std() will return the standard
    deviation of each column
  • Variance (defined as the square of the standard
    deviation) is calculated using the var() function
  • var(X) Calcuate the variance of vector x
  • If x is a matrix, var() will return the standard
    deviation of each column

49
Standard Error of the Mean
  • In Class Exercise 1
  • Create a function called se that calculates the
    standard error of some vector supplied to the
    function
  • Eg. se(x) should return the standard error of
    matrix x

50
Data Correlations
  • Matlab can calculate statistical correlations
    using the corrcoef() function
  • R,P corrcoef(A,B)
  • Calculates a matrix of R correlation
    coefficiencts and P significance values (95
    confidence intervals) for variables A and B
  • A B
  • R A AcorA BcorA
  • B AcorB BcorB

51
Part B Statistics Toolbox
  • The Statistics tool box contains a large array of
    statistical tools.
  • This lecture will concentrate on some of the most
    commonly used statistics for research
  • Parametric and non-parametric comparisons
  • Curve Fitting

52
Comparison of Means
  • A wide variety of mathametical methods exist for
    determining whether the means of different groups
    are statistically different
  • Methods for comparing means can be either
    parametric (assumes data is normally distributed)
    or non-parametric (does not assume normal
    distribution)


53
Parametric Tests - TTEST
  • H,P ttest2(X,Y)
  • Determines whether the means from matrices X and
    Y are statistically different.
  • H return a 0 or 1 indicating accept or reject nul
    hypothesis (that the means are the same)
  • P will return the significance level


54
Parametric Tests - TTEST
  • Example
  • For the data from exercise 3
  • H,P ttest2(var1,var2)
  • gtgt H,P ttest2(var1,var2)
  • H 1
  • P 0.00000000000014877

Variable 1
Variable 2

55
Curve Fitting
  • A least squares linear fit minimizes the square
    of the distance between every data point and the
    line of best fit
  • P robustfit(X,Y) returns the vector B of the y
    intercept and slope, obtained by performing
    robust linear fit

56
Curve Fitting
  • Plotting a line of best fit in Matlab can be
    performed using either a traditional least
    squares fit or a robust fitting method.

12
10
8
6
Least squares

4
Robust
2
0
-2
1
2
3
4
5
6
7
8
9
10
Write a Comment
User Comments (0)
About PowerShow.com