Econ 520: An Introduction To Matlab: 1 Getting Started
Econ 520: An Introduction To Matlab: 1 Getting Started
Keisuke Hirano
In this course you will be using Matlab to perform numerical computations for the problem
sets. Matlab is a matrix programming language: a variable is considered a matrix by default, and
matrix operations (such as matrix multiplication, transpose, inverse, etc.) are built in. Since it is
a programming language, it gives you a lot of control over its calculations, but you must be rather
precise in telling it what to do. This short guide is intended to help you get started using Matlab,
but it only scratches the surface of what you can do with it. Much more information can be found
in the Matlab manual, which also contains a very useful tutorial section.
1 Getting Started
If you are using the Windows version of Matlab, many operations can be done using the pull-down
menus. However, for all platforms, you can type commands directly to the Matlab intrepreter at
the >> prompt.
A very useful command is help. If you just type help and hit return, you will be presented with
a menu of help options. If you type help command, Matlab will provide help for that particular
command. For example, help hist will call up information on the hist command, which produces
histograms. In addition, you can use the Help menu at the top of the Matlab window to get the
Table of Contents and Index of commands.
You might have to change the working directory (using the command cd) to tell Matlab where
your files are (or will be) located. The commands pwd and dir may also be useful for file manage-
ment.
To end a Matlab session, type exit or quit.
1
Second, variables in Matlab are treated as matrices. For example, the command X*Y is a matrix
multiplication, and works only if the two matrices are conformable. To perform array multiplication,
type X.*Y. This multiplies the (a,b) element of X with the (a,b) element of Y for all (a,b), and so
requires the two matrices to be of the same size. X’ means the transpose of X.
Suppose a and b are scalars (1 × 1 matrices). You can refer to the (a,b) element of the matrix X
by X(a,b). In a similar vein, X(:,b) refers to the b’th column of X, and X(a,:) refers to the a’th
row of X. You can get even fancier: X(1:10,b) refers to the 10 by 1 vector comprised of X(1,b)
through X(10,b). Suppose that i is a m × 1 vector of integers between 1 and n (repeats are ok),
and X is an n × k matrix. Then X(i,:) is the m × k matrix formed by selecting rows of X according
to i.
You can also perform logical indexing of a matrix. If i is an n × 1 vector of logical (true-false)
values, X is an n × k matrix, X(i,:) returns the matrix formed by only including the rows of X
that correspond to a value of 1 in the vector i. As an example of a logical vector, suppose that
a is an n × 1 vector of numbers. The expression (a > 0) returns an n × 1 logical vector; the i’th
element of (a > 0) is equal to true if a(i) > 0 and is equal to false otherwise. A vector of ones
and zeros can be turned into a logical vector by using the logical command.
For small matrices, we can define them by typing something like:
a=[1 2 3; 4 5 6; 7 8 9]
To define a new matrix it will sometimes be useful to use a command along the lines of
X = [ones(n,1) z];
This will create a matrix X by putting an n × 1 vector of ones in front of the matrix z. The variable
z must have exactly n rows.
Note that some commands, including sqrt, log, and exp, perform calculations element–by–
element. (There are different commands for the matrix counterparts, but you probably won’t be
needing those.)
2
1. Interactive Mode
2. Batch Mode
3
chol cd std find
inv dir pwd
At the Matlab prompt, you would type prog and hit return. Matlab would read prog.m, then
perform the commands in order: first generate 100 independent standard normal draws, then
multiply those by 2 and add 3, then graph the histogram of these draws (using 20 bins). The first
line is ignored because it starts with %; it’s a good idea to comment your code extensively with %.1
Keep in mind that the M–file must be a text file, not, for example, a Word file. (If you write
the M–file in a word processor, just remember to save it as a text file.) If you are using Matlab for
Windows, you can use the New M-file and Open M-file options in the File menu to work with
M–files.
When handing in problem sets, you should include a copy of the M–files used to solve them.
4 Functions
Matlab lets you define new functions, which can then be called from within a Matlab session or
from a batch file. The function needs to reside in the current working directory or along Matlab’s
search path (which can be displayed and modified with the command path).
Here is an example of a function in Matlab. The file containing the function should be called
bernoulli.m.
1
If you are experiencing difficulties printing an M–file that starts with %, try adding an extra blank line at the
top of the file. Postscript printers may misinterpret the file type if the M–file starts with %.
4
function y=bernoulli(p,n);
% BERNOULLI.M
% This function generates n independent draws of a Bernoulli
% random variable with probability of success p.
This function takes two arguments, p and n, and returns a vector of n independent Bernoulli
random variables with probability of success equal to p. Notice that I have commented the file
extensively. The function can then be called from a Matlab interactive session, or from within
another batch file or another function. For example, the following interactive session calls the
bernoulli function:
>> p=.7;
>> y=bernoulli(p,1000);
>> mean(y)
ans = 0.70800
This calls bernoulli.m to generate 1000 independent Bernoulli random variables with p = .7.
The average of these 1000 variables is .708, quite close to the expected value .7.
Note: when defining functions, you can have it return more than one variable. For example,
you could define a function func1.m that takes as inputs x1 and x2, and returns y1 and y2. The
first line of the function definition would be:
y1=x1-x2;
y2=x1*(x2-x1);
% end function definition