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


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.0013957147080457
Result from 'min' function  :       0.0013957147080457


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.5031610702515940
Result from 'mean' function  :       0.5031610702515940


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

7.6606e-02   2.2477e-04   1.7211e-08   1.6162e-12   4.5504e-19   1.7179e-22

Columns 7 through 10

8.8866e-27   3.9819e-30   1.1165e-35   1.2029e-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

7.6606e-02   2.2477e-04   1.7211e-08   1.6162e-12   4.5504e-19   1.7179e-22

Columns 7 through 10

8.8866e-27   3.9819e-30   1.1165e-35   1.2029e-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.3073640559442278
Result from 'std' function  :       0.3073640559442278


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.4887471864344226
Median using 'median' :        0.4887471864344226


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.9878522740469704
Result from 'max' function  :       0.9878522740469704


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

7.6606e-02   6.5998e+00   1.1954e+01   1.6989e+01   2.1499e+01   2.6903e+01

Columns 7 through 10

3.1617e+01   3.7099e+01   4.1590e+01   4.6534e+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

7.6606e-02   6.5998e+00   1.1954e+01   1.6989e+01   2.1499e+01   2.6903e+01

Columns 7 through 10

3.1617e+01   3.7099e+01   4.1590e+01   4.6534e+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  :   1.2546911632408109e-45
Result from 'prod' function :   1.2546911632408109e-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_4_solns.m) Published with MATLAB® 8.2