Professional Documents
Culture Documents
Matlab Introductory Course: Barcelona Graduate School of Economics
Matlab Introductory Course: Barcelona Graduate School of Economics
Tatjana Dahlhaus
tatjana.dahlhaus@uab.cat
Defining a vector:
rowvector: » V=[1 2 3 4]
columnvector: » V=[1; 2; 3; 4]
Defining a matrix:
» B=[1 2 3 4; 5 6 7 8]
B is a 2 × 4 matrix, i.e.,
1 2 3 4
B=
5 6 7 8
Defining a 3D matrix:
» A(:,:,1)=B
» A(:,:,2)=[9 10 11 12; 13 14 15 16]
A is 2 × 4 × 2 matrix.
You can access the element at the ith row and the jth
column by typing B(i,j)
Accessing ith row B(i,:)
Accessing jth column B(:,j)
Accessing ith to (i+c)th row and jth to (j+n)th column
B(i:i+c,j:j+n)
Accessing last row and last column B(end,end)
diag(B)extracts diagonal elements of a matrix.
tril(B)creates lower triangular matrix.
triu(B)creates upper triangular matrix.
Consider matrix
1 2 3
B = 5 6 7
9 4 8
» F=B(1:2,2:3)
yields
2 3
F =
6 7
» F=B(2:end,[1 3])
yields
5 7
F =
9 8
1 » ones(50,1)*5
2 » [1:30 (-30:-1)*-1]
» [1:30 30:-1:1]
3 » A(A<0)=0
» A(A>0)=1
4 » A(1,find(A(1,:)<0))=0
5 » A(find(A<0|A>=5))=0
6 » A(find(A>=2&A<=5))=0
7 It does not produce an index but a logical vector in which 1
stand for ’true’ and 0 stands for ’false’.
Transpose of a Matrix:
Define
1 2
B = 5 6
9 4
The Transpose of B can be obtained by »A=B’ and yields
1 5 9
A=
2 6 4
Addition: +
Substraction: −
Multiplication: ∗
Right Division: / (B/A = BA−1 )
Left Division: \ (A \ B = A−1 B)
Exponentiation:ˆ (Aˆ(2) = A2 )
Array Operations (element-by-element): To indicate an
array operation the standard operator is preceded by a dot,
i.e., .* ./ .ˆ
» x=[1 2 3]
» y=[4 5 6]
» x.*y
ans =
4 10 18
1 reshape(A’,9,1)
2 c=a.*b
Matlab can also read data from .xls files directly (no need
to transform everything into ASCII format).
data = xlsread(filename, sheet)
reads the specified worksheet, where sheet is either a
positive, double scalar value or a quoted string containing
the sheet name.
data = xlsread(filename, ’range’)
reads data from a specific rectangular region of the default
worksheet (Sheet1). Specify range using the syntax
’C1:C2’, where C1 and C2 are two opposing corners that
define the region to be read. For example, ’D2:H4’
represents the 3-by-5 rectangular region between the two
corners D2 and H4 on the worksheet. The range input is
not case sensitive and uses Excel A1 notation.
4. Mathematical Functions
5. Random Numbers
√
log e4 /2
1 Compute 2π .
2 Write a command that will simulate the n throws of a pair of
dice.
3 Given a 20 × 2 matrix of iid N(1,2) random vectors, build
the corresponding demeaned values.
4 Build two N × 1 vectors k and y where each entry of y is
yt = 3 + 0.5X1t + 0.9X2t + ǫt where Xit is Normal(i, 2) and
ǫt is Normal(0, 1) and kt = 3 + 0.5X1t + 0.9X2t Using the
standard OLS formula estimate the coefficients,
1 X11 X21
′ −1 ′ .. .. .. for
i.e.,β̂ = (X X ) X y where X = . . .
1 X1N
N = 50, 1000, 5000. What do you observe?
1 » (sqrt(log(exp(4)))/2)/(2*pi)
2 » floor(1+6*rand(n,2))
3 » rv=1+2*randn(10,2)
» Demeaned=rv-ones(10,1)*mean(rv)
6. Graphics
» load scatterplot
» plot(data(:,5),data(:,3),’b.’)
» plot(data(:,4),data(:,3),’k*’)
» plot(data(:,4),data(:,5),’cx’)
BGSE Matlab Introductory Course
6.3 2-D Scatterplot
2 2
For example, to evaluate the function x exp−x −y over the
range −2 < x < 2, −2 < y < 2,
» [X,Y] = meshgrid(-2:.2:2, -2:.2:2);
» Z = X .* exp(-X.ˆ 2 - Y.ˆ 2);
» surf(X,Y,Z)
» mesh(X,Y,Z)
contour3(X,Y,Z) produces a 3-D contours plot.
zlabel creates a label for the z-axis.
1 Plot y = sin 3πx using fplot and plot for 0 <= x <= 1.
2 Write a function that calculate the Standard Deviation of a
matrix.
3 Write a function that will simulate N throws of 2 dice and
returns the mean of the throws.
4 Build a N × 1 vector y where each entry of y is
yt = a + bX1t + cX2t + ǫt where Xit is Normal(i, 2) and ǫt is
Normal(0, 1). Write a function which calculates OLS
estimates for β of the model y having as inputs N, a, b and
c.
4.
function Beta = OLSSimulation(N,a,b,c)
% returns OLS estimates from simulated data
regression.
X1=1+2*randn(N,1);
X2=2+2*randn(N,1);
Y=a+b*X1+c*X2+randn(N,1);
X=[ones(N,1) X1 X2];
Beta=inv(X’*X)*X’*Y;
Example: x=-1+2*rand(100,1);
for i=1:1:100
if x(i)>0
d(i)=1;
elseif x(i)<0
d(i)=-1;
else
d(i)=0;
end
end
5.
S=1; n=1;
while S+(n+1)2 < 100
n=n+1;
S=S+n2 ;
end
»[n,S]
6.
R=randn(100,1);
for t=1:size(R,1)
if t>5
if R(t)>mean(R(t-5:t))
Buy(t)=1;
else
Buy(t)=0;
end
end
end