Introduction To MATLAB: Simon O'Keefe Non-Standard Computation Group Sok@cs - York.ac - Uk
Introduction To MATLAB: Simon O'Keefe Non-Standard Computation Group Sok@cs - York.ac - Uk
Simon O’Keefe
Non-Standard Computation Group
[email protected]
Content
2
1 Introduction to MATLAB
3
What are we interested in?
n Matlab is too broad for our purposes in this
course.
n The features we are going to require is
Matlab
Series of
Matlab
commands
Command
m-files mat-files
Line
n Optimization
n To Start
q On Windows XP platform select
q Start->Programs->Maths and Stats->
MATLAB->MATLAB_local->R2007a->MATLAB R2007a
q For access to the Genetic Algorithms and Stats
toolboxes, you must use R2007b on Windows
n MATLAB runs on Linux quite happily but we do not have
toolbox licences
n To stop (nicely)
q Select File -> Exit MATLAB
q Or type quit in the MATLAB command window
1 The MATLAB interfaces
Workspace
Command Window
Command History
8
1 Window Components
entered.
10
2 Variables, vectors and matrices
11
2.1 Creating Variables
n Variables
q Names
n Can be any string of upper and lower case letters along with
numbers and underscores but it must begin with a letter
n Reserved names are IF, WHILE, ELSE, END, SUM, etc.
n Names are case sensitive
q Value
n This is the data the is associated to the variable; the data is
accessed by using the name.
q Variables have the type of the last thing assigned to
them
n Re-assignment is done silently – there are no warnings if you
overwrite a variable with something of a different type.
12
Variables
n No need for types. i.e.,
int a;
double b;
float c;
14
2.1 Single Values
15
2.1 Single Values
16
2.1 Vectors
n A vector is a list of numbers
q Use square brackets [] to contain the numbers
17
2.1 Vectors
18
2.1 Vectors
n A row vector can be converted into a column vector
by using the transpose operator ‘
19
2.1 Matrices
21
2.1 Matrices
n You can also use built in functions to create a matrix
>> A = zeros(2, 4)
creates a matrix called A with 2 rows and 4 columns
containing the value 0
>> A = zeros(5) or >> A = zeros(5, 5)
creates a matrix called A with 5 rows and 5 columns
22
2.1 Clearing Variables
n You can use the command “clear all” to delete all
the variables present in the workspace
23
2.2 Accessing Matrix Elements
24
2.2 Accessing Matrix Elements
1st
2nd
Excel
MATLAB
2nd 1st
25
2.2 Changing Matrix Elements
26
2.2 Accessing Matrix Rows
27
2.2 Accessing Matrix Columns
28
2.2 Changing Matrix Rows or Columns
n These reference methods can be used to change the
values of multiple matrix elements
29
2.2 Changing Matrix Rows or Columns
n To overwrite a row or column with new values
>> results(3, :) = [10, 1, 1, 1]
>> results(:, 3) = [1; 1; 1; 1; 1; 1; 1]
NOTE: Unless you are overwriting with a single value the data entered
must be of the same size as the matrix part to be overwritten.
30
2.2 Accessing Multiple Rows, Columns
q To access consecutive Rows or
Columns use : with start and
end points:
31
2.2 Accessing Multiple Rows, Columns
n To access multiple non consecutive
Rows or Columns use a vector of
indexes (using square brackets [])
32
2.2 Changing Multiple Rows, Columns
33
2.3 Copying Data from Excel
q Double click on the variable you would like to store the data
in
n This will open the array editor
q In the Array Editor right click in the first element and select
“Paste Excel Data”
34
2.3 Copying Data from Excel
35
2.4 The colon operator
n The colon : is actually an operator, that generates a row
vector
n This row vector may be treated as a set of indices when
accessing a elements of a matrix
n The more general form is
q [start:stepsize:end]
>> [11:2:21]
11 13 15 17 19 21
>>
n Stepsize does not have to be integer (or positive)
>> [22:-2.07:11]
22.00 19.93 17.86 15.79 13.72 11.65
>>
2.4 Concatenation
38
3 More Operators
39
3.1 Mathematical Operators
40
3.1 Mathematical Operators
n Simple mathematical operations are easy in MATLAB
41
3.1 Mathematical Operators
42
3.1 Mathematical Operators
43
3.1 Mathematical Operators
n Combining this with methods from Accessing Matrix Elements
gives way to more useful operations
>> results = zeros(3, 5)
>> results(:, 1:4) = rand(3, 4)
>> results(:, 5) = results(:, 1) + results(:, 2) + results(:, 3) + results(:,
4)
or
>> results(:, 5) = results(:, 1) .* results(:, 2) .* results(:, 3) .* results(:,
4)
NOTE: There is a simpler way to do this using the Sum and Prod
functions, this will be shown later.
44
3.1 Mathematical Operators
>> results = zeros(3, 5)
>> results(:, 1:4) = rand(3, 4)
>> results(:, 5) = results(:, 1) + results(:, 2) + results(:, 3) + results(:,
4)
45
3.1 Mathematical Operators
n Example:
46
3.1 Operation on matrices
q Logical Operators:
n Greater Than: >
n Less Than: <
n Greater Than or Equal To: >=
n Less Than or Equal To: <=
n Is Equal: ==
n Not Equal To: ~=
48
3.2 Logical Indexing
n ind is the same size as r and contains zeros (false) where the
data does not fit the criteria and ones (true) where it does, this
is called a Logical Vector.
n r(ind) then extracts the data where ones exist in ind
49
3.2 Logical Indexing
>> r = results(:,1)
>> ind = r > 0.2
>> r(ind)
50
3.3 Boolean Operators
q Boolean Operators:
n AND: &
n OR: |
n NOT: ~
51
3.3 Boolean Operators
52
4 Functions
53
4 Functions
54
4 Functions
n You can also tell the function to store the result in parts of
a matrix
>> matrix(:, 5) = function_Name(matrix(:, 1:4))
55
4 Functions
56
4 Functions
n MATLAB has many built in functions which make it easy to perform a
variety of statistical operations
q sum – Sums the content of the variable passed
57
4 Special functions
>> mean([1,2,3,4,5])
=3
59
4 Functions
60
4 Functions
n When using std, max and min you need to write:
>> function_Name(input, [], 2)
61
4 Functions
n From Earlier
>> results(:, 5) = results(:, 1) + results(:, 2) + results(:, 3) + results(:,
4)
or
>> results(:, 5) = results(:, 1) .* results(:, 2) .* results(:, 3) .* results(:,
4)
62
4 Functions
n More usefully you
can now take the
mean and standard
deviation of the
data, and add them
to the array
63
4 Functions
64
4 Functions
n We can use functions and logical indexing to extract all the
results for a subject that fall between 2 standard deviations of
the mean
>> r = results(:,1)
>> ind = (r > mean(r) – 2*std(r)) & (r < mean(r) + 2*std(r))
>> r(ind)
65
Use of M-File
Click
to
create
a
new
M-‐File
•
Extension
“.m”
•
A
text
file
containing
script
or
function
or
program
to
run
Use of M-File Save
file
as
Denem430.m
n Which takes the square of the input matrix if the input
indicator is equal to 1
n And takes the element by element square of the input
matrix if the input indicator is equal to 2
Same Name
Writing User Defined Functions
n Another function which takes an input array and returns the sum and product
of its elements as outputs
n The function sumprod(.) can be called from command window or an m-file as
5 Plotting
71
5 Plotting
72
5 Plotting
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
73
5 Plotting
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1 2 3 4 5 6 7 8 9 10
74
5 Plotting
q legend
q axis
q xlabel
q ylabel
75
5 Plotting
>> x = [0:0.1:2*pi]; Sin Plots
>> y = sin(x); 2
sin(x)
>> plot(x, y, 'b*-') 1.5 2*sin(x)
>> hold on 1
>> xlabel(‘x’);
-1.5
>> ylabel(‘y’);
-2
0 1 2 3 4 5 6
>> hold off x
76
5 Plotting
n Plotting data
0.9
0.8
0.7
0.6
0.5
0.4
77
5 Plotting
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0 2 4 6 8 10 12
78
5 Plotting
n You can close all the current plots using ‘close all’
79
Notes:
n “%” is the neglect sign for Matlab
(equaivalent of “//” in C). Anything after it on
the same line is neglected by Matlab
compiler.
n Sometimes slowing down the execution is
done deliberately for observation purposes.
You can use the command “pause” for this
purpose
pause %wait until any key
pause(3) %wait 3 seconds
Useful Commands
>>lookfor keyword