You are on page 1of 6

Intro to Matlab: Solutions to

Exercises
Monique Ebell
Studienzentrum Gerzensee

Exercise 1 Create a 3x1 matrix, call it MY_ONES, and …ll it with ’1’ s.

MY_ONES = [1; 1; 1]

Exercise 2 Create a 3x2 matrix and …ll it with whatever strikes your
fancy.

FUN = [11 32; 42 40; 71 73]

Exercise 3 Take the Row 2, Column 1 element of matrix A and call it


b.

b = A(2,1)

Exercise 4 Create a 2x2 matrix from the last two columns of matrix A.
Call it A3.

A3 = A(:,2:3)

Exercise 5 Create a 1x3 matrix from the second row of matrix A. Call
it
I_CAN_CALL_THIS_WHATEVER_I_WANT.

I_CAN_CALL_THIS_WHATEVER_I_WANT = A(2,:)

Exercise 6 Create a 4x4 identity matrix.

ID4 = eye(4)

1
Exercise 7 Subtract matrix C from matrix D.

D-C =
1 4
2 5
3 6

Exercise 8 Say that the vector MONIQUE from page 2 represents monthly
observations on a measure of consumer con…dence. Find the average
of these observations using the matrix and scalar operations described
above.

Sum_M = ones(1,3)*MONIQUE
160.8
mean_M = Sum_M/3
53.6

Exercise 9 Run mean_of_Z.m. What problem do you encounter?

The program does not display the solution !

Exercise 10 Combine the statements you used to …nd the mean of the
consumer con…dence measure over the past three months into a little
program. Call it my_conf.m. Add statements that will display the results
and run it.

MONIQUE = [54.4;52.9;53.5];
Sum_M = ones(1,3)*MONIQUE;
Mean_M = Sum_M/3;
disp(’Mean consumer confidence ==’);
disp(Mean_M);

Exercise 11 Does it matter whether Y has 1 column or 3 or 10? Com-


bine the three series into one matrix called BADWEEK as follows: the
…rst column corresponds to the 5 daily closes of Renault, the second to
those of Telefonica and the third to those of Intel. Now apply the func-
tion my_mean to the new matrix. Display your output and …gure out what
it represents.

No, it does not matter how many columns Y has. To see this, try out
the following program:

2
RENAULT = [62.40; 57.60; 54.10; 53.00; 54.55];
TELEFONICA = [17.90; 18.55; 18.42; 18.37; 18.10];
INTEL = [29.44; 27.75; 29.25; 29.06; 28.50];
BADWEEK = [RENAULT TELEFONICA INTEL];
all_means = my_mean(BADWEEK);
disp(’all_means ==’);
disp(all_means);

The output of this program will be:

all_means =
56.3300 18.2680 28.8000

The output is a row vector, each element of which gives the mean of
the corresponding column of the matrix BADWEEK.

Exercise 12 The skewness of a series X 0 = [x1 ; x2 ; :::xT ] is its third


central moment. That is, sample skewness sbx is given by
T µ ¶3
1 X xt ¡ ¹bx
sbx =
T t=1 bx
¾

where ¹bx is the sample mean and ¾bx is the sample standard deviation.
Write a function that …nds the skewness of a series. Write a program
that tries it out on RENAULT, TELEFONICA and INTEL. Hint: look
in the help menus for an explanation of the function std().

function sk = my_skewness(x)
% this functions computes the skewness of a sample
mu = mean(x);
sig = std(x);
r = (x-mu)/sig;
sk = mean(r.^3);
Now try the function out:

RENAULT = [62.40; 57.60; 54.10; 53.00; 54.55];


R_skewness = my_skewness(RENAULT);
disp(’renault skewness ==’);
disp(R_skewness);

3
The output of this program will be:

R_skewness ==
0.6282

Notice that this function cannot be used on matrices, but only on col-
umn vectors. If you feel like challenging yourself, try to write a function
that accepts any t by k matrix.

Exercise 13 The kurtosis of a series X 0 = [x1 ; x2 ; :::xT ] is its fourth


central moment. That is, sample kurtosis b
kx is given by
T µ ¶4
1 X xt ¡ ¹bx
sbx =
T t=1 bx
¾

where ¹bx is the sample mean and ¾bx is the sample standard deviation.
Write a function that …nds the kurtosis of a series. Write a program
that tries it out on RENAULT, TELEFONICA and INTEL. Hint: look
in the help menus for an explanation of the function std().

function ku = my_kurtosis(x)
% this functions computes the skewness of a sample
mu = mean(x);
sig = std(x);
r = (x-mu)/sig;
ku = mean(r.^4);
Now try the function out:

RENAULT = [62.40; 57.60; 54.10; 53.00; 54.55];


R_kurtosis = my_kurtosis(RENAULT);
disp(’renault kurtosis ==’);
disp(R_kurtosis);

The output of this program will be:

R_kurtosis ==
1.4595

Notice that this function cannot be used on matrices, but only on col-
umn vectors. If you feel like challenging yourself, try to write a function
that accepts any t by k matrix.

4
Exercise 14 Write a for loop that adds up the numbers from 1 to 100.

sum_all = 0.0;
for i=1:100
sum_all = sum_all + i;
end
disp(’sum_all ==’);
disp(sum_all);

The output will be:

sum_all ==
5050

Exercise 15 Write a for loop that adds up the even numbers from 1
and 100. HINT: you may …nd the modulus operator helpful. mod(x,y) is
the remainder of xy .

sum_evens = 0;
for i=1:100
if (mod(i,2) == 0)
sum_evens = sum_evens + i;
end
end
disp(’sum_evens ==’);
disp(sum_evens);

The output will be:

sum_evens ==
2550

Exercise 16 Before you try this loop out, think through it. What will
the result be? De…nitely more than 20? De…nitely less than 20? Or
impossible to say?

The loop on page 14 of the text will keep running until x >= 20.
Hence, at the end of the program, the value of x will be greater than or
equal to 20.

5
Exercise 17 Write a loop that adds up random numbers, but makes
sure that the sum of random numbers x de…nitely stays below 20.0.

x = 0.0;
while ( x < 20)
y = randn(1,1);
x_new = x + y;
if (x_new >= 20)
break;
else
x = x_new;
end
end
disp(x);

Exercise 18 Run the program consisting of the four statements above


[on page 18]. What do you notice?

The program reads the matrix into a row vector. It also …rst reads
the …rst column, and then the second column. This gives the output:
1.0000 3.0000 2.0000 4.0000
This is probably not a convenient form!

Exercise 19 Now add a second fprintf statement, namely:


fprintf(fp,’%f %f nn’, N); and run the program. What do you no-
tice now?

Answer in text.

Exercise 20 Add yet another fprintf statement, namely one that will
cause the matrix N to appear as a column vector.

fprintf(fp, ’%fnn’,N);

You might also like