% LMex020209_4th.m
% Example 2.2.9 page 28 in 
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% Written by Eugene.Gallagher@umb.edu 1/14/11 44
% http://alpha.es.umb.edu/faculty/edg/files/edgwebp.html
% Let A be the event that the sum of the faces is odd. Let B be the event
% that the two faces themselves are odd. What is the intersection?
% Use the dice example LMex020202_4th.m as a paradigm
green=1:6
red=green'
% tile rows down 6 times
green=repmat(green,6,1)  % repmat is a built-in function
% for earlier versions of matlab, I used rowcopy and colcopy.m
% tile columns across 6 times
red=repmat(red,1,6) % Note that the 1 column green is replaced
                        % by a 6x6 matrix.
sumdice=red+green
[r,c]=size(sumdice);  % returns the number of rows and columns
i=find(mod(sumdice,2));  % Returns the remainder after dividing by two
[A,m]=sort(sumdice(i));           %finds all of the odd sums, at location i
fprintf('Event A consists of the following dice and their sum:\n');
disp('    red  green  sum')
disp([red(i(m)) green(i(m)) A])
j=find(mod(red,2)& mod(green,2));
B=[red(j) green(j)]
AintB=intersect(i,j);
if isempty(AintB)
    fprintf('The intersection of A and B is empty.\n')
else
    fprintf('The intersection of A and B has %2.0f outcomes:\n',...
        length(AintB))
        disp('    red  green  sum')
    disp([red(AintB) green(AintB) sumdice(AintB)])
end