## Code and Output

### 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:

``````ii = 2;
j = 3;
k = ii^j   % ii 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
qq = 2^pi``````

### Output 4.1.4:

``````pp =
1.1822
qq =

8.8250``````

### Code 4.2.1:

``q = sqrt(m)``

### Output 4.2.1:

``q = 8``

### Code 4.2.2:

``remPiOver3 = rem(pi,3)``

### Output 4.2.2:

``````remPiOver3 =
0.1416``````

### Code 4.2.3:

``````subject_number = 7;
remainder = rem(subject_number,2)``````

### Output 4.2.3:

``````remainder =
1``````

### Code 4.2.4:

``abs([-1 3 -5 7])``

### Output 4.2.4:

``````ans =
1     3     5     7``````

### Code 4.2.5:

``k = exp(5)``

### Output 4.2.5:

``````k =
7.3891``````

### Code 4.2.6:

``exp(1)``

### Output 4.2.6:

``````ans =
2.7183``````

### Code 4.2.7:

``````e = exp(1)
e = 12``````

### Output 4.2.7:

``````e =
2.7183
e =
12``````

### Code 4.2.8:

``log(k)``

### Output 4.2.8:

``````ans =
5``````

### Code 4.2.9:

``````log2of_128 = log2(128)
two_tothe_7 = 2^7

log10of_1000 = log10(1000)
ten_tothe_3 = 10^3``````

### Output 4.2.9:

``````log2of_128 =
7
two_tothe_7 =
128

log10of_1000 =
3
ten_tothe_3 =
1000``````

### Code 4.2.10:

``````valueof_ln_30 = log(30)
e_tothe_3point4012 = exp(valueof_ln_30)

valueof_log2_30 = log2(30)
two_tothe__4point9069 = 2^ valueof_log2_30

valueof_log10_30 = log10(30)
ten_tothe_1point4771 = 10^ valueof_log10_30``````

### Output 4.2.10:

``````valueof_ln_30 =
3.4012
e_tothe_3point4012 =
30.0000
valueof_log2_30 =
4.9069
two_tothe__4point9069 =
30.0000
valueof_log10_30 =
1.4771
ten_tothe_1point4771 =
30.0000``````

### Code 4.2.11:

``log5(625)``

### Output 4.2.11:

``````>> log5(30)
??? Undefined function or variable 'log5'.``````

### Code 4.2.12:

``````logtobase5of_625 = log(625)/log(5)
FiveTotheFourth = 5^4``````

### Output 4.2.12:

``````logtobase5of_625 =
4
FiveTotheFourth =
625``````

### Code 4.2.13:

``sqrt(-1)``

### Output 4.2.13:

``````ans =
0 + 1.0000i``````

### Code 4.2.14:

``````imaginary = sqrt(1*-1)
complex1 = 2*imaginary
complex2 = imaginary + (0 + 3i)
complex3 = imaginary – 3
complex4 = complex1+complex2-complex3``````

### Output 4.2.14:

``````imaginary =
0 + 1.0000i
complex1 =
0 + 2.0000i
complex2 =
0 + 4.0000i
complex3 =
-3.0000 + 1.0000i
complex4 =
3.0000 + 5.0000i``````

### Code 4.2.15:

``````clear all
% Review of special numbers other than NaN, namely, 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
clear i
After_Clearing_i = i``````

### Code 4.2.15:

``````The_Special_Number_Pi =
3.1416
The_Special_Number_Sqrt_Minus_1 =
0 + 1.0000i
i_Redefined =
10
After_Clearing_i =
0 + 1.0000i``````

### 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.``````

### Code 4.4.1:

``uniform_random_distribution = rand(4,8)``

### Output 4.4.1:

``````uniform_random_distribution =
0.6225    0.4709    0.2259    0.3111    0.9049    0.2581    0.6028    0.2967
0.5870    0.2305    0.1707    0.9234    0.9797    0.4087    0.7112    0.3188
0.2077    0.8443    0.2277    0.4302    0.4389    0.5949    0.2217    0.4242
0.3012    0.1948    0.4357    0.1848    0.1111    0.2622    0.1174    0.5079``````

### Code 4.4.2:

``uniform_integer_distribution = randi(5,1,8)``

### Output 4.4.2:

``````uniform_integer_distribution =
2     2     5     1     4     1     2     3``````

### Code 4.4.3:

``normaldistribution = randn(4,8)``

### Output 4.4.3:

``````normaldistribution =
1.7249   -0.9441   -0.2948    0.1133    0.0619    0.4322   -0.0327   -0.8380
-1.0620    0.0485    1.0637   -1.2334    1.7941    0.1206   -0.1556    0.2336
0.8708   -0.5808    1.1224   -1.0238    0.7657   -1.9044    0.8514    0.5481
1.4471    0.3301    1.6000   -0.9096    0.1164    1.1801    0.8001    1.3894``````

### Code 4.4.4:

``````mu = 10;
stdev = 15;
new_distribution = (normaldistribution * stdev) + mu``````

### Output 4.4.4:

``````new_distribution =
5.1879   10.3649   17.0985    4.1751  -16.7249    8.6445    7.5049  -10.1910
-8.9439   -0.0138   25.9044   18.3431    9.5045   -5.8584   -8.7725    1.1299
8.7720    4.9679   34.5157   13.6902    0.2338   12.1307    7.8305    8.9384
43.4989  -12.2061    4.8443   -1.8680    8.8181   29.9599   26.2904   19.0650``````

### Code 4.4.5:

``oneSubjectsOrder = randperm(8)``

### Output 4.4.5:

``````oneSubjectsOrder =
6     2     3     8     7     1     5     4``````

### Code 4.4.6:

``````r = randperm(32)
permutedIntegers = reshape(r,4,8)``````

### Output 4.4.6:

``````permutedIntegers =
4     2     9    28     1    25    30    11
3    12    14    22     7    29    27    21
26    20    13    24    18    23    16    19
6    17    32    31    15    10     8     5``````

### Code 4.4.7:

``````a = randi(8,1,10)
b = randi(8,1,10)
rng('default')   % or rand('twister',5489) for earlier releases
c = randi(8,1,10)``````

### Output 4.4.7:

``````a =
1     8     1     7     7     7     1     4     3     7
b =
4     8     2     3     2     2     7     5     5     2
c =
1     8     1     7     7     7     1     4     3     7``````

### Code 4.4.8:

``````rng('default')
d = randi(8,1,10)
rng('default')
rng('shuffle')
currentRandomNumberState = rng;
e = randi(8,1,10)``````

### Output 4.4.8:

``````d =
1     8     1     7     7     7     1     4     3     7
e =
6     4     4     6     4     1     8     4     5     2``````

### Code 4.4.9:

``````rng('shuffle')
rng(currentRandomNumberState);
f = randi(8,1,10)``````

### Output 4.4.9:

``````f =
6     4     4     6     4     1     8     4     5     2``````

### Code 4.5.1:

``````r = [7 33 39 26 8 18 15 4 0];
sum_r = sum(r)
mean_r = mean(r)
median_r = median(r)
standard_deviation_r = std(r)
variance_r = var(r)
minimum_r = min(r)
maximum_r = max(r)``````

### Output 4.5.1:

``````sum_r =
150
mean_r =
16.6667
median_r =
15
standard_deviation_r =
13.5277
variance_r =
183
minimum_r =
0
maximum_r =
39``````

### Code 4.5.2:

``````r = randi(10,3,5)
sum_vector = sum(r)
mean_vector = mean(r)
median_vector = median(r)
standard_deviation_vector = std(r)
variance_vector = var(r)
minimum_vector = min(r)
maximum_vector = max(r)``````

### Output 4.5.2:

``````r =
2     8     1     7     4
5    10     9     8     7
10     7    10     8     2

sum_vector =
17    25    20    23    13
mean_vector =
5.6667    8.3333    6.6667    7.6667    4.3333
median_vector =
5     8     9     8     4
standard_deviation_vector =
4.0415    1.5275    4.9329    0.5774    2.5166
variance_vector =
16.3333    2.3333   24.3333    0.3333    6.3333
minimum_vector =
2     7     1     7     2
maximum_vector =
10    10    10     8     7``````

### Code 4.5.3:

``````s = [1:20];
t = [50:-1:31];
correlation_matrix = corrcoef(s,t)
r = correlation_matrix(1,2)``````

### Output 4.5.3:

``````correlation_matrix =
1    -1
-1     1
r =
-1``````

### Code 4.6.1:

``````r = randi(10,3,5);
r(3,3:4) = NaN;
r(1:2,2:3) = NaN;
r
sum_vector = sum(r)
mean_vector = mean(r)``````

### Output 4.6.1:

``````r =
2   NaN   NaN     7     4
5   NaN   NaN     8     7
10     7   NaN   NaN     2

sum_vector =
17   NaN   NaN   NaN    13
mean_vector =
5.6667       NaN       NaN       NaN    4.3333``````

### Code 4.6.2:

``````Data = [1 NaN 4 3 NaN 4]
Data_that_are_not_Nans = Data(not(isnan(Data)))
Mean_of_Data_without_Nans = mean(Data_that_are_not_Nans)``````

### Output 4.6.2:

``````Data =
1   NaN     4     3   NaN     4
Data_that_are_not_Nans =
1     4     3     4
Mean_of_Data_without_Nans =
3``````

### Code 4.6.3:

``````r
Column_Means = nanmean(r)
Column_Standard_Deviations = nanstd(r)``````

### Output 4.6.3:

``````r =
2   NaN   NaN     7     4
5   NaN   NaN     8     7
10     7   NaN   NaN     2

Column_Means =
5.6667    7.0000       NaN    7.5000    4.3333

Column_Standard_Deviations =
4.0415         0       NaN    0.7071    2.5166``````

### Code 4.7.1:

``````u = [1:6]
v = u + 20``````

### Output 4.7.1:

``````u =
1     2     3     4     5     6
v =
21    22    23    24    25    26``````

### Code 4.7.2:

``w = v - 20``

### Output 4.7.2:

``````w =
1     2     3     4     5     6``````

### Code 4.7.3:

``x = w * 2``

### Output 4.7.3:

``````x =
2     4     6     8    10    12``````

### Code 4.7.4:

``y = x / 2``

### Output 4.7.4:

``````y =
1     2     3     4     5     6``````

### Code 4.7.5:

``````Z1 = [1:6;7:12]
Z2 = Z1 + 2``````

### Output 4.7.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.7.6:

``Z3 = Z1 + Z2``

### Output 4.7.6:

``````Z3 =
4     6     8    10    12    14
16    18    20    22    24    26``````

### Code 4.7.7:

``````Z4 = Z1 - 2
Z5 = Z1 – Z2``````

### Output 4.7.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.7.8:

``````aa = [1:4;5:8]
bb = [4:-1:1;8:-1:5]
cc = aa .* bb``````

### Output 4.7.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.7.9:

``dd = aa ./ bb``

### Output 4.7.9:

``````dd =
0.2500    0.6667    1.5000    4.0000
0.6250    0.8571    1.1667    1.6000``````

### Code 4.7.10:

``dd = aa .^ .25``

### Output 4.7.10:

``````dd =
1.0000    1.1892    1.3161    1.4142
1.4953    1.5651    1.6266    1.6818``````

### Code 4.7.11:

``````dd3 = dd * 3
halfdd = dd/2``````

### Output 4.7.11:

``````dd3 =
3.0000    3.5676    3.9483    4.2426
4.4859    4.6953    4.8798    5.0454
halfdd =
0.5000    0.5946    0.6581    0.7071
0.7477    0.7825    0.8133    0.8409``````

### Code 4.7.12:

``````allFives = 5 * ones(2,7)
allNans = NaN * ones(2,5)``````

### Output 4.7.12:

``````allFives =
5     5     5     5     5     5     5
5     5     5     5     5     5     5
allNans =
NaN   NaN   NaN   NaN   NaN
NaN   NaN   NaN   NaN   NaN``````

### Code 4.7.13:

``````x = [1:8];
y = x.^2
d1y = diff(y)
d2y = diff(d1y)
d3y = diff(d2y)``````

### Output 4.7.13:

``````y =
1     4     9    16    25    36    49    64
d1y =
3     5     7     9    11    13    15
d2y =
2     2     2     2     2     2
d3y =
0     0     0     0     0``````

### Code 4.8.1:

``````R = [
cos(pi/6) -sin(pi/6)
sin(pi/6)  cos(pi/6)
]

### Output 4.8.1:

``````R =
0.8660   -0.5000
0.5000    0.8660
1
0
0.8660
0.5000
-0.8660
0.5000`````` ### Code 4.8.2:

``````radiusrotatedFourMoreTimes_Complex = ...

### Output 4.8.2:

``````radiusrotatedFourMoreTimes_Complex =
-0.8660 + 0.5000i``````

### Code 4.8.3:

``````vectorR1 = [1,0];  % originalradiuspoint
vectorR2 = [0.8660,    0.5];   % radiusrotatedOnce
dotR1R2 = dot(vectorR1, vectorR2)

### Output 4.8.3:

``````dotR1R2 =
0.8660
0.5236
RotationR1R2_degrees =
30.0029``````

### Code 4.8.4:

``````vectorR1 = [1,0]; % originalradiuspoint
vectorR3 = [-0.8660,    0.5];  % radiusrotatedFourMoreTimes
dotR1R3 = dot(vectorR1, vectorR3)

### Output 4.8.4:

``````dotR1R3 =
-0.8660
2.6179
RotationR1R3_degrees =
149.9971``````

### Code 4.8.5:

``````vectorR1 = [1,0,0];
vectorR3 = [-0.8660,    0.5,0];
AxisOfRotation = cross(vectorR1,vectorR3)``````

### Output 4.8.5:

``````AxisOfRotation =
0         0    0.5000``````

### Code 4.9.1:

``````r = [3 1 2]
sorted_r = sort(r)``````

### Output 4.9.1:

``````r =
3     1     2
sorted_r =
1     2     3``````

### Code 4.9.2:

``````rr = [randperm(10)' randperm(10)']
srr1 = sort(rr)``````

### Output 4.9.2:

``````rr =
10     9
9     4
5     5
1     2
4    10
2     6
7     7
8     8
6     1
3     3
srr1 =
1     1
2     2
3     3
4     4
5     5
6     6
7     7
8     8
9     9
10    10``````

### Code 4.9.3:

``````srr2 = sortrows(rr,1)
srr3 = sortrows(srr2,-2)``````

### Output 4.9.3:

``````srr2 =
1     2
2     6
3     3
4    10
5     5
6     1
7     7
8     8
9     4
10     9
srr3 =
4    10
10     9
8     8
7     7
2     6
5     5
9     4
3     3
1     2
6     1``````

### Code 4.9.4:

``````subject = randperm(6)
thedata = randi(8,6);
mydata = [subject; thedata]
mydataSortedbySubject = [sortrows(mydata',1)]'``````

### Output 4.9.4:

``````subject =
2     3     4     1     6     5
mydata =
2     3     4     1     6     5
6     3     5     4     7     7
5     7     4     1     1     5
6     3     8     1     6     1
4     4     7     5     8     6
3     2     6     7     6     5
4     2     7     7     5     7
mydataSortedbySubject =
1     2     3     4     5     6
4     6     3     5     7     7
1     5     7     4     5     1
1     6     3     8     1     6
5     4     4     7     6     8
7     3     2     6     5     6
7     4     2     7     7     5``````

### Code 4.10.1:

``````dd = [
1.0000    1.1892    1.3161    1.4142
1.4953    1.5651    1.6266    1.6818]
round(dd)``````

### Output 4.10.1:

``````dd =
1.0000    1.1892    1.3161    1.4142
1.4953    1.5651    1.6266    1.6818
ans =
1     1     1     1
1     2     2     2``````

### Code 4.10.2:

``floor(dd)``

### Output 4.10.2:

`````` ans =
1     1     1     1
1     1     1     1``````

### Code 4.10.3:

``ceil(dd)``

### Output 4.10.3:

``````ans  =
1     2     2     2
2     2     2     2``````

### Code 4.10.4:

``````fix_dd = fix(dd)
fix_minus_dd = fix(-dd)
floor_minus_dd = floor(-dd)``````

### Output 4.10.4:

``````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``````

### Code 4.10.5:

``````disp('         a   floor(a)   fix(a)  round(a)   ceil(a)');
a = (-2:.25:2)';
b = [a floor(a) fix(a) round(a) ceil(a)];
disp(b)``````

### Output 4.10.5:

``````         a  floor(a)    fix(a)  round(a)   ceil(a)
-2.0000   -2.0000   -2.0000   -2.0000   -2.0000
-1.7500   -2.0000   -1.0000   -2.0000   -1.0000
-1.5000   -2.0000   -1.0000   -2.0000   -1.0000
-1.2500   -2.0000   -1.0000   -1.0000   -1.0000
-1.0000   -1.0000   -1.0000   -1.0000   -1.0000
-0.7500   -1.0000         0   -1.0000         0
-0.5000   -1.0000         0   -1.0000         0
-0.2500   -1.0000         0         0         0
0         0         0         0         0
0.2500         0         0         0    1.0000
0.5000         0         0    1.0000    1.0000
0.7500         0         0    1.0000    1.0000
1.0000    1.0000    1.0000    1.0000    1.0000
1.2500    1.0000    1.0000    1.0000    2.0000
1.5000    1.0000    1.0000    2.0000    2.0000
1.7500    1.0000    1.0000    2.0000    2.0000
2.0000    2.0000    2.0000    2.0000    2.0000``````

### Code 4.11.1:

``````n = 4;
magic(n)``````

### Output 4.11.1:

``````ans =
16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1``````

### Code 4.11.2:

``calendar(1776,7)``

### Output 4.11.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``````

### Code 4.12.1:

``````rng('default')
A = randi(9,3,3)
sum(A)``````

### Output 4.12.1:

``````A =
8     9     3
9     6     5
2     1     9
ans =
19    16    17``````

## Solutions

``````% Solutions_Chapter_04

% Solutions for selected problems from MATLAB for Behavioral Scientists,
% Second Edition (D. A. Rosenbaum, J. Vaughan, & B. Wyble),
% (c) 2015, Taylor & Francis

% To generate the solution for one problem, copy and run the code for that
% problem in a file or paste it into the Command window. Show the Command
% window to see the results.

% To generate sll the solutions for Chapter 4, save this code as a
% MATLAB script file and run the program.

% Problem 4.12.1
% You have done a study on the effects of five different treatments on the
% performance of two groups of participants. One group had earlier exposure
% to the task, causing their mean score to be 15 points higher than for the
% first-time group. The data for the two groups are as follows:
First_Time_Group = [71 78 80 86 91];
Second_Time_Group = [86 91 97 97 110];
%
% What single line of code will remove the 15-point advantage for the
% Second_Time_Group?
clc
commandwindow
fprintf('\n\n          %s\n\n','Output 4.12.1')
Second_Time_Group_Corrected = Second_Time_Group - 15

% Problem 4.12.2
% Continuing with the study of the two groups and using the original
% values of First_Time_Group and the transformed value of  Second_Time_Group,
% compute the mean and standard deviation of First_Time_Group, the mean and
% standard deviation of Second_Time_Group, and the mean and standard
% deviation of the paired differences between First_Time_Group and
% Second_Time_Group. Use variable names that will make it easy to
% understand the output.
fprintf('\n\n          %s\n\n','Output 4.12.2')
fprintf('Solution left to the student.\n\n');

% Problem 4.12.3
% Assign random permutations of 8 treatments, numbered 1 to 8, to each of
% 4 participants.
fprintf('\n\n          %s\n\n','Output 4.12.3')
Treatments = [randperm(8);randperm(8);randperm(8);randperm(8);]
% OR...
Treatments(1,1:8) = randperm(8);
Treatments(2,1:8) = randperm(8);
Treatments(3,1:8) = randperm(8);
Treatments(4,1:8) = randperm(8);
Treatments

% Problem 4.12.4
% Amy participated in a gymnastics competition. She received the following
% scores in each of 4 events.
% Vault: 8.9, 8.7, 8.2, 9.1, 9.0
% Uneven_bar: 9.5, 9.3, 9.3, 9.25, 8.9
% Balance_beam: 8.9, 8.9, 8.7, 8.6, 8.5
% Floor: 8.9, 8.8, 8.8, 8.7, 8.9
% Amy's final score for any given event needs to be the mean score after
% removing the lowest and highest score for that event. Write a program that
% computes Amy's final score in each apparatus and then the total of all her
% final scores.
fprintf('\n\n          %s\n\n','Output 4.12.4')
Vault = [8.9, 8.7, 8.2, 9.1, 9.0];
Uneven_bar = [9.5, 9.3, 9.3, 9.25, 8.9];
Balance_beam = [8.9, 8.9, 8.7, 8.6, 8.5];
Floor = [8.9, 8.8, 8.8, 8.7, 8.9];
Vault_sort = sort(Vault);
Uneven_bar_sort = sort(Uneven_bar);
Balance_beam_sort = sort(Balance_beam);
Floor_sort = sort(Floor);

TotalScore = mean(Vault_sort(2:4)) + ...
mean(Uneven_bar_sort(2:4)) + ...
mean(Balance_beam_sort(2:4)) + ...
mean(Floor_sort(2:4))

% Problem 4.12.5
%
% You are preparing stimuli for an experiment and discover a mistake in
% the final column of values on which the stimuli will be based. Each value
% in the final column needs to be squared. Write a program to correct the
% error using just one command. Don't just square each mistaken value by
% hand. The data that need to be corrected are as follows:
fprintf('\n\n          %s\n\n','Output 4.12.5')
fprintf('Solution left to the student.\n\n');

% Problem 4.12.6
%
% Use MATLAB to verify that the right column of magic(3) sums to 15, and
% that each diagonal of magic(3) sums to 15.
fprintf('\n\n          %s\n\n','Output 4.12.6')
m3 = magic(3);
Sum_C3 = sum(m3(:,3))
Sum_Main_Diag = sum([m3(1,1) m3(2,2) m3(3,3)])
Sum_Sec_Diag = sum([m3(3,1) m3(2,2) m3(1,3)])

% Problem 4.12.7
%
% Earvin "Magic" Johnson wore jersey number 32 when he played basketball
% for the Los Angeles Lakers. Verify that the top row and rightmost
% column of magic(32) both sum to the same number. Can you think
% of an easy way to check the sum of all the columns with one
% command? Likewise for the sum of all the rows? How about checking
% the main diagonal? (Hint: type doc diag in the command window).
% Now compute the sum of the secondary diagonal (top right to bottom
% left), as efficiently as possible. Hint: type doc fliplr in the
% command window.
fprintf('\n\n          %s\n\n','Output 4.12.7')
fprintf('Solution left to the student.\n\n');

% Problem 4.12.8
% Generate a set of 1000 scores (satscores) in a 100×10 table that
% are normally distributed as ideal SAT scores (mean 500, sd = 100).
% Verify that the mean and standard deviation of each column are near
% 500 and 100, respectively.
% SOLUTION NOTE: the exact answers will vary with the state of the random
% number generator!
fprintf('\n\n          %s\n\n','Output 4.12.8')
satscores = round(randn(100,10)*100)+500;
ColMeans = mean(satscores)
GrandMean = mean(mean(satscores))
ColStds = std(satscores)
MeanStd = mean(std(satscores))
% satscores

% Problem 4.12.9
% Make four 1×1000 matrices of random numbers, v1, v2, v3, and v4,
% using randn, and compute W1 = v1 + v2, W2 = v1 + v3, and W3 = v3 + v4.
% What are the correlations among W1, W2, and W3? If you've had a
% statistics course, explain the difference between the values of these
% correlations in terms of the sources of variability for W1, W2, and W3.
fprintf('\n\n          %s\n\n','Output 4.12.9')
v1 = randn(1,1000);
v2 = randn(1,1000);
v3 = randn(1,1000);
v4 = randn(1,1000);
W1 = v1 + v2;
W2 = v1 + v3;
W3 = v3 + v4;
c12 = corrcoef(W1,W2);
c13 = corrcoef(W1,W3);
c23 = corrcoef(W2,W3);
r12 = c12(1,2)
r13 = c13(1,2)
r23 = c23(1,2)

% Problem 4.12.10
%
% Create SATs, a 1400 ×2 matrix of normally distributed random
% multiples of 10 (i.e. 200, 210, 220, etc.) with a mean of 500
% and a standard deviation of 100, using randn, round, and other
% operations. Don?t print it out! Find the mean and standard deviation
% of each column.
fprintf('\n\n          %s\n\n','Output 4.12.10')
fprintf('Solution left to the student.\n\n');

% Problem 4.12.11
% The following code generates a 3×3 matrix, A, and reports the sum of
% the columns of A as a row.
% Code 4.12.11:
fprintf('\n\n          %s\n\n','Output 4.12.11')
rng('default')
A = randi(9,3,3);
sum(A);
%
% Output 4.12.11:
% A =
%      8     9     3
%      9     6     5
%      2     1     9
% ans =
%     19    16    17
%
% Add exactly one character to the command sum(A), to report
% the sum of the rows of A, as a row.
SumRowsAinaColumn = sum(A')
% Add exactly two characters to the command sum(A), to again report the
% sum of the rows of A, but as a column. (There are two solutions to this one!)
SumRowsAinaRow_1 = sum(A')'
SumRowsAinaRow_2 = sum(A,2)
% Add exactly three characters to the command sum(A), to report the sum
% of all elements of A.
SumAllofA_1 = sum(A(:))
% Add exactly five characters to the command sum(A), to again report the
% sum of all elements of A.
SumAllofA_2 = sum(sum(A))

% Problem 4.12.12
%
% For the statistically minded, the matrix A, generated in Code 4.12.11
% just after the random number generator was initiated, has three 9's,
% but no 4 or 7. Use MATLAB to compute the probability of the next
% (or any future) such matrix having no repeated digits.
% SOLUTION NOTE: For all the digits to be different, the first can be any
% digit, the second can be any of the eight remaining digits, the third can
% be any of the seven remaining, and so forth. One solution is simply to
% multiply the probabilities of "success" for each digit:
fprintf('\n\n          %s\n\n','Output 4.12.12')
fprintf('Solution left to the student.\n\n');
fprintf('Probability_1 = \n');
fprintf('    9.366567084168850e-04');

% Problem 4.12.13
%
% In Section 3.6 (Code 3.6.9), we noted that each value of a logarithmic
% series, after the first one, is a constant multiple of the prior value.
% Verify that this is the case (i.e., show the ratio between each element
% and the next) for logseries1 = logspace(1,2,5), using shorthand notation
% for matrix indices (i.e., by writing one MATLAB statement).
fprintf('\n\n          %s\n\n','Output 4.12.13')
logseries1 = logspace(1,2,5);
SeriesRatios = logseries1(2:end)./logseries1(1:end-1)
commandwindow``````

### Output 4.12.1

``````Second_Time_Group_Corrected =
71    76    82    82    95``````

### Output 4.12.2

``Solution left to the student.``

### Output 4.12.3

``````Treatments =
7     2     8     5     6     4     1     3
5     4     8     2     6     1     7     3
7     5     8     3     4     6     2     1
7     1     2     5     8     4     3     6
Treatments =
4     1     6     5     7     8     2     3
6     5     2     7     4     3     1     8
3     5     1     6     2     7     4     8
3     4     7     5     2     8     6     1``````

### Output 4.12.4

``````TotalScore =
35.7167``````

### Output 4.12.5

``Solution left to the student.``

### Output 4.12.6

``````Sum_C3 =
15
Sum_Main_Diag =
15
Sum_Sec_Diag =
15``````

### Output 4.12.7

``Solution left to the student.``

### Output 4.12.8

``````ColMeans =
Columns 1 through 7
487.5700  496.7500  489.5400  497.6500  508.6600  495.0500  487.1000
Columns 8 through 10
480.9500  502.2000  518.5500
GrandMean =
496.4020
ColStds =
Columns 1 through 7
107.2484   91.1866   98.7010   93.9349   92.4733   95.9993   97.3658
Columns 8 through 10
94.8484  100.7596  109.0598
MeanStd =
98.1577``````

### Output 4.12.9

``````r12 =
0.5282
r13 =
-8.3286e-04
r23 =
0.4518``````

### Output 4.12.10

``Solution left to the student.``

### Output 4.12.11

``````SumRowsAinaColumn =
20    20    12
SumRowsAinaRow_1 =
20
20
12
SumRowsAinaRow_2 =
20
20
12
SumAllofA_1 =
52
SumAllofA_2 =
52``````

### Output 4.12.12

``````Your answer should be:
Probability_1 =
9.366567084168850e-04``````

### Output 4.12.13

``````SeriesRatios =
1.7783    1.7783    1.7783    1.7783``````