程序代写代做代考 AI Bayesian interpreter chain flex gui matlab algorithm Excel Introduction to Matlab

Introduction to Matlab

Christopher K. I. Williams

Division of Informatics, University of Edinburgh
October 1999

Background

This document has the objective of introducing you to some of the facilities available in
Matlab.
The exercises are

1. Using the interpreter and help system.

2. Plotting facilities.

3. Scripts and functions.

4. Matrices.

Section 5 gives information on the Netlab toolbox for neural networks.
Matlab is an interpreted language for numeric computation and visualisation. It offers

high level facilities for dealing directly with mathematical constructs. The particular benefits
that it offers for this course are:

1. Excellent support for linear algebra and matrix operations. The basic type inMatlab
is a double precision matrix. The software was originally developed as a linear algebra
package (Matlab stands for MATrix LABoratory) and has efficient and numerically
reliable algorithms for matrix inversion, eigenvalues etc.

2. Visualisation facilities. The in-built graphing and plotting functions are easy to use
for both 2d and 3d plots.

3. Ease of extension. Functions and scripts can be written in the Matlab language (in
‘M-files’) and these can then be called in exactly the same way as the core functionality
of Matlab. In fact, the ‘toolboxes’ that extend the functionality of Matlab to
more specialist areas are written in this way. Netlab (a Matlab toolbox for neural
networks) consists of a set of M-files.

4. Portability. Software written in the Matlab language is portable to any platform
that runs Matlab, including Unix machines, PCs and Macintoshes. This means that
Netlab can be used to train networks on high performance machines and then run
on lower performance machines.

1

1 Using the Matlab interpreter and help system

The basic objects that Matlab works with are matrices : 2-d rectangular arrays of double
precision (or complex) numbers. Operations and commands in Matlab are intended to
work with matrices just as they would be written down on paper. The Matlab interpreter
can be controlled dynamically from the command line.

Instructions:

1. To enter a matrix, you should follow these conventions:

• Separate entries with white space or commas.

• Use a semi-colon ; to denote the end of each row.

• Surround the entries with square brackets [ and ].

The statement

A = [8 1 6; 3 5 7; 4 9 2]

results in the output

A =

8 1 6

3 5 7

4 9 2

Matlab will output the result of every command. To suppress this, you should ter-
minate your command with a semi-colon ;. Thus, typing

A = [8 1 6; 3 5 7; 4 9 2];

still sets A to be the same matrix, but there is no output.

The value of any variable, such as the matrix A, is retained until it is explicitly modified.
If there is no variable on the left hand side of an expression, thenMatlab assigns the
result to a built-in variable named ans.

To find out the value of the i, jth element of A, type A(i, j). For example, on typing

b = A(1, 2)

the following is output

2

b =

1

Note that matrix indices start from 1. To obtain the second row of the matrix, type

c = A(2, 🙂

which gives the following output:

c =

3 5 7

To obtain the third column, type

d = A(:, 3)

which gives the following output:

d =

6

7

2

2. In some situations, Matlab gives a special meaning to vectors, which are matrices
with only one row or column. A particularly useful way of forming row vectors is using
the colon operator :. The line

x = 0:0.1:1;

generates a row vector x of length 11 containing the values from 0 to 1 with increments
of 0.1. This construction is often used to generate the x-axis points in graphs. So the
command

y = sin(2 * pi * x);

generates regularly spaced values from a sine curve.

3. The Matlab interpreter allows you to edit your command line. This makes it much
easier to correct small typing mistakes or to repeat sequences of nearly identical com-
mands without error. You can use emacs-style command line editing, e.g. Ctl-p gives
you the previous command, and this can be repeatedly used to go back in the command

3

history. You can then edit a command line using the usual delete/backspace key, and
move around in the command line using Ctl-b (moves backwards) and Ctl-f (forwards).

Alternatively, the up and down arrow keys (usually on the right of your keyboard)
can be used to move forwards or backwards through the command history, while left
and right arrow keys position the cursor on the command line. Typing carriage return
anywhere on the line will then submit the command to the interpreter.

For example, to modify the variable x, type up arrow twice. This will give you the
same line as you typed two commands ago, namely:

x = 0:0.1:1;

By moving the cursor, deleting the characters between the two colons, and typing 0.05,
modify this to read

x = 0:0.05:1;

and type carriage return to submit the command. Now the vector x has length 21
and contains the values from 0 to 1 with increments of 0.05. However, y still has
length 11, and its values no longer correspond correctly with those in x. (Unlike with
a spreadsheet, we cannot link variables in Matlab so that they are automatically
updated.) To rectify this, the simplest solution is to repeat the definition of y by
typing up arrow twice and submitting the command.

4. Of course, most of the time the datasets we will be analysing are far too large to type
at the command line without error. Instead, we can read in the matrices from external
files. These files can be in a Matlab format, or stored as text, with each row of the
matrix on a new line. To read in some data which is stored in Matlab format, type

! cp /hame/ckiw/Public/matlab_intro/tecator.mat .

load tecator

This copies the file tecator.mat into the current working directory (! is a shell-escape
command), and then reads in six matrices stored in it. You can find out what variables
you currently have by typing:

who

To save data, you can use the command save. For example, to save the variable A in
ASCII format into a file A.dat, type:

save A.dat A -ascii

Type ls to get a listing of the files in your current directory, which should include
A.dat.

4

5. Matlab is a very powerful tool with hundreds of built-in functions. Even the most
hardenedMatlab veteran may occasionally forget the name of a function or the order
of its arguments. Fortunately, there is an on-line help facility that provides information
on most topics. Later in the course you will also be able to get help on the neural
networks toolbox Netlab by the same mechanism.

To get help on a specific function, you need only type help followed by the function
name. So, to find out about the function mean, you should type:

help mean

A more general information search is provided by lookfor (which is similar to the Unix
apropos command). This enables you to find out what functions Matlab provides
for certain operations. For example, to find out what functions there are related to
calculating covariances, type:

lookfor covariance

2 Simple use of two dimensional plotting facilities

Instructions:

1. A dataset for a toy regression problem (the noisy sin) can very easily be plotted using
the vector x we created earlier. The simplest form of the plot function takes the x
vector as its first argument, and the y vector as its second argument. Different line
styles (or scatter plots) can be defined with a third argument. (For more detail, just
type help plot.) To see the data, type

plot(x, sin(2*pi*x) + 0.1*randn(size(x)), ’+’)

which will give you a scatter plot of the data with yellow plusses for each point. The
function randn generates samples from a standard normal distribution (i.e. a Gaussian
with zero mean and unit variance).

Notice that the function sin can take a vector (or indeed a matrix) as input, and
return the sine of each element.

Say we also wanted to plot another curve on the same axes, e.g. cos 2πx. We need two
new ideas: (i) the use of hold on, which permits multiple plots to be made on the same
axes and (ii) the choice of different colours for different plots. This can be achieved by
a third argument to the plot function ( for more detail, just type help plot).

5

plot(x, sin(2*pi*x))

hold on

plot(x, cos(2*pi*x), ’g’)

hold off

The ’g’ option plots the cosine curve in green. hold off cancels the hold on com-
mand.

It is possible to add extra information to the plot using the title, xlabel, ylabel, and
text commands, and to control the scaling of the axes using the axis command. Note
that these commands must follow the plot to which they refer. Axes with non-linear
scaling can be obtained with the loglog, semilogx and semilogy commands.

A plot can easily be saved in a variety of formats. For example, to save a plot as an
encapsulated postscript file called myplot.eps, type

print -deps myplot.eps

2. We can also easily make histogram plots. First, create some data by typing

z = randn(500,1)

This will create a vector of 500 (pseudo)random samples from a zero-mean unit-variance
Gaussian distribution. We will now plot a histogram of this data, specifying 15 bins
by typing

hist(z, 15)

Does it look how you expect it should?

3 Elementary use of scripts and functions.

In some exercises you will need to edit existing scripts and functions, either to test the effect
of varying parameters, or to make it easier to run a sequence of instructions multiple times
(in case of typing errors). This exercise is designed to give you some practice at this.
Scripts automate a fixed sequence of instructions. Functions are more flexible, in that

they can take arguments and return values, which allows the user to abstract away from
particular variable names. Both functions and scripts are text files (called M-files) which
have the name foo.m, where foo is the script or function name. Functions and scripts are
invoked inMatlab by typing name with the relevant arguments enclosed in round brackets.

Instructions:

6

1. Copy the file /hame/ckiw/Public/matlab_intro/whiten.m to an appropriate direc-
tory.

2. Start up a text editor.

3. Open the file whiten.m. The purpose of this function is to pre-process data by applying
a linear transformation so that all the variables are zero mean and unit variance. The
first line of the file

function y = whiten(x)

defines the function interface. The argument x is the input data matrix, while the
return value y is the processed data (and therefore has the same dimensions as x).
The usualMatlab convention is that each column represents a variable and each row
represents an observation. To return multiple values, the following syntax is used:

function [y1, y2, y3] = whiten(x1, x2)

The variables x and y are local to this function and

are quite distinct from the variables we have already defined in this session.

4. Note that the % symbol indicates that the rest of the line is a comment and will be
ignored by the Matlab interpreter. The line

n = size(x, 1);

finds the number of rows in the data matrix. After this, add the lines

mu = mean(x);

s = std(x);

to calculate the mean and standard deviation of each column of the data. The next
step is to subtract the mean of each column. While it would be possible to do this using
Matlab’s for loops, it would be very slow. It is much more efficient to use matrix
operations. What is required is to subtract a matrix where every entry in the ith
column is the mean of that column. This can be constructed by adding the following
lines to the file:

e = ones(n, 1);

y = x – e*mu;

Now y is a data matrix where each variable has zero mean. We just need to divide
each column by the standard deviation. To do this, we use the element-wise division
operator ./ (the usual division operator a/b essentially multiplies a by the matrix
inverse of b).

7

y = y ./ (e*s);

Once you have added these lines, save the file.

5. You are now ready to run the function. Switch back to theMatlab command window.
First, we can generate some random data:

foo = rand(20, 5);

The function rand generates samples from a random variable which is

uniformly distributed on the interval [0, 1]. This dataset has 20 rows and 5 columns.
Type

m = mean(foo)

s = std(foo)

to find out the mean and standard deviation of each variable. Now we can normalise
the data with our function. Type

bar = whiten(foo);

and find out the new mean and standard deviation. To within rounding error, they
should be zero and one respectively.

4 Matrices

4.1 Matrix addition and multiplication

Enter a matrix by typing

A = [8 1 6; 3 5 7; 4 9 2]

This results in the output

A =

8 1 6

3 5 7

4 9 2

We can also enter matrices B and C by typing B = [1 4 7; 2 5 8; 3 6 0] and
C = [1 2 3; 4 5 6]. Does the statement D = A + B do what you would expect? How
about E = A + C?
To transpose a matrix in Matlab we use the ’ operator (a single backquote). For

example, try F = A’ .
Suppose we define a column vector by g = [-1 0 2]’. Then we multiply this vector by

a matrix by typing h = A*g. Suppose we create a row vector, e.g. u = g’. What happens
if we then try v = A*u ? Why ?

Matlab will, of course, also multiply matrices of the appropriate size. For example
A2 = A*B is valid, as is A3 = C*A. What about A4 = A*C ?

8

4.2 Solving systems of linear equations

Suppose we want to solve the system of linear equations represented by

Ax = g.

Matlab provides a special division operator for this purpose; we can simply write

x = A g

and the solution,

x =

-0.0194

0.2722

-0.1861

is printed out. You can check that x is the solution to the system of linear equations by
calculating A*x.

4.3 Manipulating the elements of a matrix

It is possible to cut parts out of matrices and perform quite complex operations cutting-and-
pasting matrix entries in Matlab. We will consider some simple examples. What is the
effect of

A

A(2:3,1:2)

Note the syntax above: 2:3 specifies the rows wanted, and 1:2 specifies the desired columns.
Rows and columns can be manipulated as complete entities. Hence A(1,:) gives the first
row of A, and A(:,2) gives the second column.

4.4 Special matrices

Some special matrices thatMatlab knows about are ones(m,n), zeros(m,n), rand(m,n),
randn(m,n) and eye(n). ones(m,n) generates a matrix of size m×n with all the elements
equal to 1. Try typing ones(4,2). zeros(m,n) acts similarly but fills all of the elements
with 0. This can be useful for initializing a matrix before computations. Try zeros(3,3).

rand(m,n), randn(m,n) generate randommatrices of sizem×n. rand choses (pseudo)random
numbers in the interval (0.0, 1.0), while randn generates them according to a Normal distri-
bution with zero mean and unit variance. Try randn(5,3).

eye(n) generates a n× n identity matrix. Type eye(5) to check this.

9

4.5 Element-by-element operations

Sometimes we don’t want to do matrix operations, but elementwise operations instead. These
are achieved by using a dot . to precede the operator. For example, say we have the vectors

y = [1 2 3]; z = [4 5 6];

What is the effect of

y .* z

y ./ z

y .^2

These “dot” operations can also be performed on matrices; for example try B./A

4.6 Output formatting

The name and value of a variable can be output just by leaving the semicolon off the end
of the line. However, it is possible to produce tidier output by using the function disp. For
example

disp(’the values in matrix A are’);

disp(A);

It is also possible to use C-like syntax with the command fprintf. The format command
can be used to specify the format for the output of numerical values. For example type

pi

format long

pi

5 Netlab neural network software

The Netlab neural network software is a toolbox for Matlab, written by Ian Nabney and
Christopher Bishop.
It consists of a library of Matlab functions and scripts based on the approach and tech-

niques described in Neural Networks for Pattern Recognition by Christopher M. Bishop,
(Oxford University Press, 1995). The functions come with on-line help, and further expla-
nation is available via HTML files which can be found at

http://www.dai.ed.ac.uk/dai/computing/software_manuals/netlabhelp/index.htm .

The Netlab homepage can be found at

http://www.ncrg.aston.ac.uk/netlab/index.html .

10

The software has been installed so that upon typing the command matlab from the
command line on School of AI unix machines, the netlab functions are ready for your use. The
command help netlab issued within MATLAB will give a listing of the various functions
and demos available, and the command demnlab will bring up a GUI interface to the demos.
A list of the demos available is given below.

dem2ddat – Generates two dimensional data for demos.

demard – Automatic relevance determination using the MLP.

demev1 – Demonstrate Bayesian regression for the MLP.

demgauss – Demonstrate sampling from Gaussian distributions.

demglm1 – Demonstrate simple classification using a generalized linear model.

demglm2 – Demonstrate simple classification using a generalized linear model.

demgmm1 – Demonstrate density modelling with a Gaussian mixture model.

demgmm2 – Demonstrate density modelling with a Gaussian mixture model.

demgmm3 – Demonstrate density modelling with a Gaussian mixture model.

demgpot – Computes the gradient of the negative log likelihood for a mixture model.

demhint – Demonstration of Hinton diagram for 2-layer feed-forward network.

demhmc1 – Demonstrate Hybrid Monte Carlo sampling on mixture of two Gaussians.

demhmc2 – Demonstrate Bayesian regression with Hybrid Monte Carlo sampling.

demhmc3 – Demonstrate Bayesian regression with Hybrid Monte Carlo sampling.

demkmean – Demonstrate simple clustering model trained with K-means.

demknn1 – Demonstrate nearest neighbour classifier.

demmdn1 – Demonstrate fitting a multi-valued function using a Mixture Density Network.

demmet1 – Demonstrate Markov Chain Monte Carlo sampling on a Gaussian.

demmlp1 – Demonstrate simple regression using a multi-layer perceptron

demmlp2 – Demonstrate simple classification using a multi-layer perceptron

demnlab – A front-end Graphical User Interface to the demos

demolgd1 – Demonstrate simple MLP optimisation with on-line gradient descent

demopt1 – Demonstrate different optimisers on Rosenbrock’s function.

dempot – Computes the negative log likelihood for a mixture model.

demprior – Demonstrate sampling from a multi-parameter Gaussian prior.

demrbf1 – Demonstrate simple regression using a radial basis function network.

demtrain – Demonstrate training of MLP network.

Acknowledgements: Section 4 owes much to Chapter 1 in “Numerical Methods Using
Matlab” by G. Lindfield and J. Penny, and to the Matlab User’s Guide.

11

Posted in Uncategorized

Leave a Reply

Your email address will not be published. Required fields are marked *