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  :      49.0233482958988418
Result from 'sum' function  :      49.0233482958988418


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.0154871256360190
Result from 'min' function  :       0.0154871256360190


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.4902334829589884
Result from 'mean' function  :       0.4902334829589884


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
6.1767e-01   2.2151e-05   1.8939e-11   5.6865e-14   3.0044e-19   6.5606e-25
Columns 7 through 10
4.5859e-29   1.1173e-31   8.5454e-37   7.6309e-41

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
6.1767e-01   2.2151e-05   1.8939e-11   5.6865e-14   3.0044e-19   6.5606e-25
Columns 7 through 10
4.5859e-29   1.1173e-31   8.5454e-37   7.6309e-41


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.2909599606766697
Result from 'std' function  :       0.2909599606766697


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.4858548495239401
Median using 'median' :        0.4858548495239401


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.9990803947613607
Result from 'max' function  :       0.9990803947613607


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
6.1767e-01   5.8339e+00   9.8101e+00   1.5987e+01   2.0214e+01   2.4285e+01
Columns 7 through 10
2.9215e+01   3.5309e+01   3.9772e+01   4.4598e+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
6.1767e-01   5.8339e+00   9.8101e+00   1.5987e+01   2.0214e+01   2.4285e+01
Columns 7 through 10
2.9215e+01   3.5309e+01   3.9772e+01   4.4598e+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  :   3.7535666987641031e-44
Result from 'prod' function :   3.7535666987641031e-44


Back to the top