Back to tutorial index

# Lab Solutions

## Introduction

For all of the results below, we will use the array x below.

clear all;
n = 100;
x = rand(1,n);   % array of 'n' random numbers between 0 and 1

Back to lab exercises.

Back to the top

## Exercise 1 - sum(x)

s = 0;
for i = 1:n,
s = s + x(i);
end
fprintf('Result from the ''for'' loop  : %24.16f\n',s);
fprintf('Result from ''sum'' function  : %24.16f\n',sum(x));
Result from the 'for' loop  :      48.5456756847693285
Result from 'sum' function  :      48.5456756847693285


Back to the top

## Exercise 2 : min(x)

xmin = 100;  % Choose a value guaranteed to be larger than the minimum value.
for i = 1:n,
xmin = min(xmin,x(i));  % Use form 'min(a,b)'
end
fprintf('Result using the ''for'' loop : %24.16f\n',xmin);
fprintf('Result from ''min'' function  : %24.16f\n',min(x));
Result using the 'for' loop :       0.0011510571291072
Result from 'min' function  :       0.0011510571291072


Back to the top

## Exercise 3 : mean(x)

s = 0;
for i = 1:n,
s = s + x(i);
end
s_mean_for = s/n;
fprintf('Result from the ''for'' loop   : %24.16f\n',s_mean_for);
fprintf('Result from ''mean'' function  : %24.16f\n',mean(x));
Result from the 'for' loop   :       0.4854567568476933
Result from 'mean' function  :       0.4854567568476933


Back to the top

## Exercise 4 : cumprod(x)

for j = 1:n,
yfor(j) = prod(x(1:j));   % prod of first j entries
end
y = cumprod(x);
format short e
fprintf('Every 10th entry, up to entry 100, from loop result : \n');
yfor(1:10:100)
Every 10th entry, up to entry 100, from loop result :

ans =

Columns 1 through 6

2.6528e-01   5.7739e-04   3.3313e-10   8.8907e-15   5.5051e-19   6.5922e-22

Columns 7 through 10

3.1698e-26   1.8625e-33   2.5888e-38   1.8903e-42


fprintf('Every 10th entry, up to entry 100, from ''cumprod'' result : \n');
y(1:10:100)
Every 10th entry, up to entry 100, from 'cumprod' result :

ans =

Columns 1 through 6

2.6528e-01   5.7739e-04   3.3313e-10   8.8907e-15   5.5051e-19   6.5922e-22

Columns 7 through 10

3.1698e-26   1.8625e-33   2.5888e-38   1.8903e-42



Back to the top

## Exercise 5 : std(x)

s = 0;
mu = mean(x);
for i = 1:n,
s = s + (x(i)-mu)^2;
end
std_for = sqrt(s/(n-1));
fprintf('Result from the ''for'' loop  : %24.16f\n',std_for);
fprintf('Result from ''std'' function  : %24.16f\n',std(x));
Result from the 'for' loop  :       0.2683102454001677
Result from 'std' function  :       0.2683102454001677


Back to the top

## Exercise 6 : length(x)

length(x)
ans =

100



Back to the top

## Exercise 7 : median(x)

y = sort(x);
median_1 = (y(n/2) + y(n/2+1))/2;
fprintf('Median using ''sort''   :  %24.16f\n',median_1);
fprintf('Median using ''median'' :  %24.16f\n',median(x));
Median using 'sort'   :        0.4724215733379660
Median using 'median' :        0.4724215733379660


Back to the top

## Exercise 8 : max(x)

xmax = -1;  % Choose a value guaranteed to be smaller than the minimum value.
for i = 1:n,
xmax = max(xmax,x(i));  % Use form 'max(a,b)'
end
fprintf('Result using the ''for'' loop : %24.16f\n',xmax);
fprintf('Result from ''max'' function  : %24.16f\n',max(x));
Result using the 'for' loop :       0.9826633997219503
Result from 'max' function  :       0.9826633997219503


Back to the top

## Exercise 9 : cumsum(x)

for j = 1:n,
yfor(j) = sum(x(1:j));   % sum of first j entries
end
y = cumsum(x);
fprintf('Every 10th entry, up to entry 100, from loop result : \n');
yfor(1:10:100)
Every 10th entry, up to entry 100, from loop result :

ans =

Columns 1 through 6

2.6528e-01   6.7027e+00   9.9844e+00   1.4854e+01   1.9673e+01   2.5465e+01

Columns 7 through 10

3.0097e+01   3.4697e+01   3.9066e+01   4.3616e+01


fprintf('Every 10th entry, up to entry 100, from ''cumsum'' result : \n');
y(1:10:100)
Every 10th entry, up to entry 100, from 'cumsum' result :

ans =

Columns 1 through 6

2.6528e-01   6.7027e+00   9.9844e+00   1.4854e+01   1.9673e+01   2.5465e+01

Columns 7 through 10

3.0097e+01   3.4697e+01   3.9066e+01   4.3616e+01



Back to the top

## Exercise 10 : prod(x)

s = 1;
for i = 1:n,
s = s*x(i);
end
fprintf('Result from the ''for'' loop  : %24.16e\n',s);
fprintf('Result from ''prod'' function : %24.16e\n',prod(x));
Result from the 'for' loop  :   2.8434182628479548e-45
Result from 'prod' function :   2.8434182628479548e-45


Back to the top

## Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_17_solns.m) Published with MATLAB® 8.5