You are on page 1of 14

MATLAB Essentials

Andreas Uthemann
UCL

October 18, 2012

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

1 / 14

Creating, Deleting & Storing Variables


create variables var1 and var2
1

var1 = 1; var2 = 2.1;

overwrite var2
1

var2 = 2.12;

store variables in file mydata.mat


1

save ( mydata . mat , var1 , var2 ) ;

clear variables
1

clear var1 var2 ;

recover variables
1

load mydata ;

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

2 / 14

Vectors

create a vector going from 2 to 10


1

myvec1 = 2:10;

vector going from 1 to 10 with 0.1 stepsize


1

myvec2 = 1:0.1:10;

100 equally spaced points between 1 and 10


1

myvec3 = linspace (1 ,10 ,100) ;

check length of vector myvec3


1

length ( myvec3 )

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

3 / 14

Matrices

create a 3 2 matrix of zeros


1

mymat1 = zeros (3 ,2) ;

transpose mymat1
1

mymat2 = mymat1 ;

check size of mymat2


1

size ( mymat2 )

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

4 / 14

Vectors and Matrices by hand and Indexing


a 1 4 Matlab vector
1

myvec4 = [1. , 2.13 , 3.1 , 4.123 ];

and a 2 4 matrix
1

mymat3 = [1 , 2 , 3 , 4; 5 , 6 , 7 , 8];

overwrite row 2, column 3 of mymat3 with 10


1

mymat3 (2 ,3) = 10;

display columns 2 to 4 of row 2


1

mymat3 (2 ,2:4)

delete first row of mymat3


1

mymat3 (1 ,:) = [];

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

5 / 14

Matrix Algebra
create 1000 3 matrix of std normal RVs and 1000 1 vector of [0, 1]
uniform RVs
1

X = randn (1000 ,3) ; y = rand (1000 ,1) ;

calculate (X T X )1 X T y
1

inv (X * X ) * X * y

multiply first column of X with y elementwise


1

X (: ,1) .* y ;

take square root of y , multiply by 3 and add 2 elementwise


1

3 .* y .^(1/2) + 2;

Dont use this to get OLS estimates. Its numerically inefficient.

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

6 / 14

Logical Operators

& (and) | (or) (not) == (equal) = (not equal) > ( strictly greater )
< ( strictly smaller ) >= ( greater equal ) <= (smaller equal)
a true statement returns 1
1

1 < 2 & ( 3 <= 2 | 2 == 2 )

a false statement returns 0


1

( 2 ~= 3 | 1 < 1 ) == 0

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

7 / 14

Conditionals

perform operation only if a condition is satisfied


1
2
3
4
5
6
7
8
9

a = 0;
if a < 0 ,
display ( a is negative )
elseif a > 0 ,
display ( a is positive )
else ,
a = 3;
display ( a was 0. it has been changed to 3 )
end

the elseif and else clause can be omitted if not necessary

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

8 / 14

Loops

for loop - repeat an operation for a specified number of times


example : create 100 periods of AR(1) process yt = 0.99yt1 + t ,
t N(0, 0.5), y0 = 1
1
2
3

y = zeros (100 ,1) ; % vector to contain y s


y (1) = 1.; % initial condition for y
epsilon = sqrt (0.5) .* randn (100 ,1) ;
% N (0 ,0.5) shocks

4
5
6
7

for t =2:100 ,
y ( t ) = 0.99 * y (t -1) + epsilon ( t ) ;
end

Note the comments in the code seperated by % . Use comments, for


others and your future self.

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

9 / 14

Loops
while loop - repeat an operation as long as a condition is true
example : run process xt = 0.99xt1 as long as |xt xt1 | > 0.0000001
for x0 = 1
1
2
3
4

x = [1.]; % vector to contain x s ,


check_condition = 1; % 1 if | x_t - x_ (t -1) | > 0.0000001
% 0 otherwise
t = 1; % time counter

5
6
7
8
9

10

while check_condition == 1 ,
x ( t +1) = 0.99 * x ( t ) ;
check_condition = abs ( x ( t +1) - x ( t ) ) > 0.0000001;
t = t + 1;
end

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

10 / 14

Plotting

plot {yt } and {xt } in a single time series plot


1

len = min ( length ( y ) , length ( x ) ) ;

% number of periods

2
3
4
5
6
7
8
9

plot (1: len , y (1: len ) , b , - ,1: len , x (1: len ) , r , -- )


% y in blue solid line ( b - )
% x in red dashed line ( r - - )
title ( time series of x and y )
ylabel ( y - axis units )
xlabel ( time )
legend ( y , x )

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

11 / 14

Result
time series of x and y

1.4

y
x

1.2
1

yaxis units

0.8
0.6
0.4
0.2
0
0.2
0.4
0.6

Andreas Uthemann (UCL)

10

20

30

40

50
time

60

MATLAB Essentials

70

80

90

100

October 18, 2012

12 / 14

Inline Functions
define the function f (x, y ) = x 2 + y 2
1

f = @ (x , y ) ( x .^2 + y .^2 ) ;

the . notation allows vectors as arguments


calculate f (2, 3)
1

f (2 ,3)

get a 10 element vector of 2 (2) RVs


1

f ( randn (10 ,1) , randn (10 ,1) )

plot f (x) = x 2 over [1, 1]


1
2
3

f = @ ( x ) x .^2;
x = linspace ( -1 ,1 ,1000) ;
plot (x , f ( x ) )

Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

13 / 14

Solving Linear Systems


problem : solve y = Xb for b with dim(y ) dim(b)
1
2

X = randn (1000 ,3) ; beta = rand (3 ,1) ;


y = X * beta + randn (1000 ,1) ;

dont do
1

b = inv (X * X ) * X * y ;

do
1

b = X \ y

if you have several y vectors, e.g.


1

y2 = X * beta + randn (1000 ,1) ; y3 = X * beta + randn (1000 ,1)

do
1

B = X \ [y , y2 , y3 ];
Andreas Uthemann (UCL)

MATLAB Essentials

October 18, 2012

14 / 14

You might also like