4. Calculations

 

4.1     Adding, subtracting, multiplying, and dividing values, and raising values to a power

4.2     Using built-in functions to compute the square root, remainder, absolute value, base of the natural logarithms raised to a power, and the logarithm

4.3     Ordering calculations

4.4     Performing statistical calculations to compute the sum, mean, standard deviation, variance, correlation, and least-squares fit

4.5     Performing statistical calculations with missing data

4.6     Calculating with matrices

4.7     Using linear algebra

4.8     Obtaining the max, min, sort, round, floor, and ceiling

4.9     Generating random numbers

4.10   Generating magic squares and calendars

4.11   Practicing calculations

 

top of page

4.1 Adding, subtracting, multiplying, dividing values, and raising values to a power

 

Code 4.1.1:

 

a = 1;

b = 2;

c = a + b   % addition

d = 1;

e = c - d   % subtraction

f = 4;

g = f * 3;  % multiplication (note the use of the asterisk, *)

h = f/g     % division

 

Output  4.1.1:

 

c =

     3

 

e =

     2

 

h =

    0.3333

 

 

 

 

Code 4.1.2:

 

i = 2;

j = 3;

k = i^j   % i raised to the j power

 

Output 4.1.2:

 

k =

     8

 

 

Code 4.1.3:

 

m = 64;

n = 1/2;    

p = m^n

 

Output 4.1.3:

 

p = 8

 

 

Code 4.1.4:

 

pp = 2^.2415

 

Output 4.1.4:

 

pp =

 

    1.1822

 

 

top of page

4.2    Using built-in functions to compute the square root, remainder, absolute value, base of the natural logarithms raised to a power, and the logarithm

 

Code 4.2.1:

 

q = sqrt(m)

 

Output 4.2.1:

 

q = 8

 

 

Code 4.2.2:

 

subject_number = 7;

remainder = rem(subject_number,2)

 

Output 4.2.2:

 

remainder =

 

     1

 

 

Code 4.2.3:

 

abs(-1)

 

Output 4.2.3:

 

ans =

     1

 

 

Code 4.2.4:

 

k = exp(2)

 

Output 4.2.4:

 

k =

    7.3891

 

Code 4.2.5:

 

exp(1)

 

Output 4.2.5:

 

ans =

 

    2.7183

 

 

Code 4.2.6:

 

e = 12

 

Output 4.2.6:

 

e =

    12.000

 

 

Code 4.2.7:

 

log(k)

 

Output 4.2.7:

 

ans =

 

     2

 

Code 4.2.8:

 

log2(30)

 

Output 4.2.8:

 

ans =

 

     4.9069

 

 

Code 4.2.9:

 

log10(30)

 

Output 4.2.9:

 

ans =

 

     1.4771

 

 

Code 4.2.10:

 

log5(30)

 

Output 4.2.10:

 

>> log5(30)

??? Undefined function or variable 'log5'.

 

 

Code 4.2.11:

 

sqrt(-1)

 

Output 4.2.11:

 

ans =

        0 + 1.0000i

 

Code 4.2.12:

 

imaginary = sqrt(1*-1)

 

Output 4.2.12:

 

imaginary =

 

        0 + 1.0000i

 

 

Code 4.2.13:

 

complex = 2*imaginary

 

Output 4.2.13:

 

complex =

 

        0 + 2.0000i

 

 

top of page

4.3 Ordering calculations

 

 

Code 4.3.1:

 

r = 2;

s = 3;

t = 4;

u = 5;

v = 6;

 

w(1) = r * s - t ^ u/v;

w(2) = r * s - (t ^ u)/v;

w(3) = r * (s - t ^ u)/v;

w(4) = r * (s - t) ^ u/v;

w(5) = (r * s) - t ^ u/v;

w(6) = (r * s - t) ^ u/v;

w(7) = (r * s - t) ^ (u/v);

w(8) = ((r * s - t) ^ u)/v;

w(9) = r * (s - t ^ u/v);

 

w’   % list w(1) through w(9) in column form

 

Output 4.3.1:

 

ans =

 

 -164.6667

 -164.6667

 -340.3333

   -0.3333

 -164.6667

    5.3333

    1.7818

    5.3333

 -335.3333

 

 

Code 4.3.2:

 

w(9) = r * (s - t ^ u/v;

 

Output 4.3.2:

 

??? w(9) = r * (s - t ^ u/v;

                           |

Error: Incomplete or misformed expression or statement.

 

 

top of page

4.4 Performing statistical calculations to obtain the sum, mean, standard deviation, variance, correlation, and least-squares fit

 

 

Code 4.4.1:

 

 r = [1:99];

 sum_r = sum(r)                   

 mean_r = mean(r)               

 median_r = median(r)      

 standard_deviation_r = std(r)   

 variance_r = var(r)

 

Output 4.4.1:

 

sum_r =

 

        4950

 

mean_r =

 

    50

 

median_r =

 

    50

 

standard_deviation_r =

 

   28.7228

 

variance_r =

 

   825

 

 

Code 4.4.2:

 

r = [1:4; 11:14];

sum_vector = sum(r)    

mean_vector = mean(r)   

median_vector = median(r) 

standard_deviation_vector = std(r)    

variance_vector = var(r)    

 

Output 4.4.2:

 

sum_vector =

 

    12    14    16    18

 

mean_vector =

 

     6     7     8     9

 

median_vector =

 

     6     7     8     9

 

standard_deviation_vector =

 

    7.0711    7.0711    7.0711    7.0711

 

variance_vector =

 

    50    50    50    50

 

 

Code 4.4.3:

 

clear r  % because it was used in the last example

s = [1:20];

t = [50:-1:31];

correlation_matrix = corrcoef(s,t)

r = correlation_matrix(1,2)

 

Output 4.4.3:

 

correlation_matrix =

 

     1    -1

    -1     1

 

r =

 

    -1

 

 

top of page

4.5 Performing statistical calculations with missing data

 

 

 

Code 4.5.1:

 

% NaN_Calculations_01

% August 7, 2006

 

clc

clear all

 

% Review of special numbers other than NaN: pi and i.

% Reminder that the default value of i can be overwritten

% but can then be restored by clearing i

The_Special_Number_Pi = pi

The_Special_Number_Sqrt_Minus_1 = i

i = 10;

i_Redefined = i

Not_Really_The_Special_Number_Sqrt_Minus_1 = i

clear i

After_Clearing_The_Special_Number_Sqrt_Minus_1 = i

 

 

% Blank_Slate filled with NaN at first, but then gets some data

% and is finally assigned to Slate_With_Data

Blank_Slate(1:4,1:4) = NaN

Blank_Slate(1,2:4) = [7 8 9];

Blank_Slate(2,1:3) = [6 7 8];

Blank_Slate(3,3:4) = [10 11];

Blank_Slate(4,1) = [12];

Slate_With_Data = Blank_Slate

 

% Statistics

Column_Means = nanmean(Slate_With_Data)

Column_Standard_Seviations = nanstd(Slate_With_Data)

 

commandwindow

 

 

Outptu 4.5.1:

 

The_Special_Number_Pi =

 

    3.1416

 

The_Special_Number_Sqrt_Minus_1 =

 

        0 + 1.0000i

 

i_Redefined =

 

    10

 

Not_Really_The_Special_Number_Sqrt_Minus_1 =

 

    10

 

After_Clearing_The_Special_Number_Sqrt_Minus_1 =

 

        0 + 1.0000i

 

Blank_Slate =

 

   NaN   NaN   NaN   NaN

   NaN   NaN   NaN   NaN

   NaN   NaN   NaN   NaN

   NaN   NaN   NaN   NaN

 

Slate_With_Data =

 

   NaN     7     8     9

     6     7     8   NaN

   NaN   NaN    10    11

    12   NaN   NaN   NaN

 

Column_Means =

 

    9.0000    7.0000    8.6667   10.0000

 

 

Column_Standard_Seviations =

 

    4.2426         0    1.1547    1.4142

 

 

top of page

4.6 Calculating with matrices

 

Code 4.6.1:

 

u = [1:6]

v = u + 20          

 

Output 4.6.1:

 

u =

 

     1     2     3     4     5     6

 

v =

 

    21    22    23    24    25    26

 

 

Code 4.6.2:

 

w = v - 20

 

Output 4.6.2:

 

w =

 

     1     2     3     4     5     6

 

 

Code 4.6.3:

 

x = w * 2

 

Output 4.6.3:

 

x =

 

     2     4     6     8    10    12

 

 

Code 4.6.4:

 

y = x / 2

 

Output 4.6.4:

 

y =

 

     1     2     3     4     5     6

 

 

Code 4.6.5:

 

Z1 = [1:6;7:12]

Z2 = Z1 + 2

 

Output 4.6.5:

 

Z1 =

 

     1     2     3     4     5     6

     7     8     9    10    11    12

 

Z2 =

 

     3     4     5     6     7     8

     9    10    11    12    13    14

 

 

 

Code 4.6.6:

 

Z3 = Z1 + Z2

 

Output 4.6.6:

 

Z3 =

 

     4     6     8    10    12    14

    16    18    20    22    24    26

 

 

Code 4.6.7:

 

Z4 = Z1 - 2

Z5 = Z1 – Z2

 

Output 4.6.7:

 

Z4 =

 

    -1     0     1     2     3     4

     5     6     7     8     9    10

 

Z5 =

 

    -2    -2    -2    -2    -2    -2

    -2    -2    -2    -2    -2    -2

 

Code 4.6.8:

 

aa = [1:4;5:8]

bb = [4:-1:1;8:-1:5]

cc = aa .* bb

 

Output 4.6.8:

 

aa =

 

     1     2     3     4

     5     6     7     8

 

bb =

 

     4     3     2     1

     8     7     6     5

 

cc =

 

     4     6     6     4

    40    42    42    40

 

 

Code 4.6.9:

 

dd = aa ./ bb                     

 

Output 4.6.9:

 

dd =

 

    0.2500    0.6667    1.5000    4.0000

    0.6250    0.8571    1.1667    1.6000

 

 

Code 4.6.10:

 

dd = aa .^ .25

 

Output 4.6.10:

 

dd =

 

    1.0000    1.1892    1.3161    1.4142

    1.4953    1.5651    1.6266    1.6818

 

 

top of page

4.8 Obtaining the max, min, sort, round, floor, and ceiling

 

 

Code 4.8.1:

 

array = [-1:.5:1];

max(array)

 

Output 4.8.1:

 

ans =

 

     1

 

 

Code 4.8.2:

 

max(dd)

 

Output 4.8.2:

 

ans =

 

    1.4953    1.5651    1.6266    1.6818

 

 

Code 4.8.3:

 

min(array)

 

Output 4.8.3:

 

ans =

 

     -1

 

 

Code 4.8.4:

 

min(dd)

 

Output 4.8.4:

 

ans =

 

    1.0000    1.1892    1.3161    1.4142

 

 

Code 4.8.5:

 

r = [3 2 1]

sorted_r = sort(r)

 

Output 4.8.5:

 

r =

 

     3     2     1

 

sorted_r =

 

     1     2     3

 

Code 4.8.6:

 

rr = [rand(10,1) randperm(10)']

srr1 = sort(rr)

 

Output 4.8.6:

 

rr =

 

    0.1870    3.0000

    0.9913    2.0000

    0.7120    8.0000

    0.8714   10.0000

    0.4796    9.0000

    0.4960    4.0000

    0.2875    7.0000

    0.0609    5.0000

    0.2625    6.0000

    0.1863    1.0000

 

srr1 =

 

    0.0609    1.0000

    0.1863    2.0000

    0.1870    3.0000

    0.2625    4.0000

    0.2875    5.0000

    0.4796    6.0000

    0.4960    7.0000

    0.7120    8.0000

    0.8714    9.0000

    0.9913   10.0000

 

Code 4.8.7:

 

srr2 = sortrows(rr,1)

srr3 = sortrows(rr,2)

 

Output 4.8.7:

 

srr2 =

 

    0.0609    5.0000

    0.1863    1.0000

    0.1870    3.0000

    0.2625    6.0000

    0.2875    7.0000

    0.4796    9.0000

    0.4960    4.0000

    0.7120    8.0000

    0.8714   10.0000

    0.9913    2.0000

 

 

srr3 =

 

    0.1863    1.0000

    0.9913    2.0000

    0.1870    3.0000

    0.4960    4.0000

    0.0609    5.0000

    0.2625    6.0000

    0.2875    7.0000

    0.7120    8.0000

    0.4796    9.0000

    0.8714   10.0000

 

Code  4.8.8:

 

[Y,ranked_rr] = sort(rr)

 

Output 4.8.8:

 

Y =

 

    0.0609    1.0000

    0.1863    2.0000

    0.1870    3.0000

    0.2625    4.0000

    0.2875    5.0000

    0.4796    6.0000

    0.4960    7.0000

    0.7120    8.0000

    0.8714    9.0000

    0.9913   10.0000

 

ranked_rr =

 

     8    10

    10     2

     1     1

     9     6

     7     8

     5     9

     6     7

     3     3

     4     5

     2     4        

 

 

Code 4.8.9:

 

round(dd)

 

Output 4.8.9:

 

ans =

 

     1     1     1     1

     1     2     2     2

 

Code 4.8.10:

 

floor(dd)

 

Output 4.8.10:

 

 ans =

 

     1     1     1     1

     1     1     1     1

 

 

Code 4.8.11:

 

ceil(dd)

 

Output 4.8.11:

 

ans  =

     2     2     2     2

     2     2     2     2

 

Code 4.8.12:

 

fix_dd = fix(dd)

fix_minus_dd = fix(-dd)

floor_minus_dd = floor(-dd)

 

Output 4.8.11:

 

fix_dd =

 

     1     1     1     1

     1     1     1     1

 

fix_minus_dd =

 

    -1    -1    -1    -1

    -1    -1    -1    -1

 

floor_minus_dd =

 

    -1    -2    -2    -2

    -2    -2    -2    -2

 

 

top of page

4.9 Generating random numbers

 

 

Code 4.9.1:

 

rand('state',sum(100*clock));

rand(2,5) 

 

Output 4.9.1:

 

ans =

 

    0.9501    0.6068    0.8913    0.4565    0.8214

    0.2311    0.4860    0.7621    0.0185    0.4447

 

Code 4.9.2:

 

randn('state',sum(100*clock));

randn(2,5)

 

Output 4.9.2:

 

ans =

 

   -0.4326    0.1253   -1.1465    1.1892    0.3273

   -1.6656    0.2877    1.1909   -0.0376    0.1746

 

 

Code 4.9.3:

 

randn('state',sum(100*clock));

mu = 10;

stdev = 15;

new_distribution = mu + (randn(2,5)*stdev)

 

Output 4.9.3:

 

new_distribution =

 

  -14.0613   -5.8471   -2.0764   13.2898  -22.5601

   13.8596   31.2271   17.9311   -3.8285    9.1122

 

Code 4.9.4:

 

r = randperm(8)

 

Output 4.9.4:

 

r =

 

     5     6     1     4     2     8     3     7

 

          

 

top of page

4.10 Generating magic squares and calendars

 

 

 

Code 4.10.1:

 

n = 4;

magic(n)

 

Output 4.10.1:

 

ans =

 

    16     2     3    13

     5    11    10     8

     9     7     6    12

     4    14    15     1

 

 

Code 4.10.2:

 

calendar(1776,7)

 

Output 4.10.2:

 

                   Jul 1776

     S     M    Tu     W    Th     F     S

     0     1     2     3     4     5     6

     7     8     9    10    11    12    13

    14    15    16    17    18    19    20

    21    22    23    24    25    26    27

    28    29    30    31     0     0     0

     0     0     0     0     0     0     0