function F = ConvDists(A)
% Syntax F = ConvDists(A)
% The array A is a common x-axis (first column) and two vectorized
% distribution functions defined over that axis (columns 2 and 3).
% The function returns the convolution of the two distributions in F.
% Col 1 of F is the expanded x axis, whose length is 1 less than twice the
% length of the x-axis vector in A; Col 2 gives the probability densities
% of the convolution distribution, normalized so that they sum to 1. The
% 2nd & 3rd cols of A must each sum to 1
% The convolution for row vectors u & v as functions of x looks like this
%
% v1 v2 v3 v4 v5 = A(:,3)
% u1 u2 u3 u4 u5 = A(:,2)
% x1 x2 x3 x4 x5 = A(:,1)
%
% u1v1 u1v2 u1v3 u1v4 u1v5
% u2v1 u2v2 u2v3 u2v4 u2v5
% u3v1 u3v2 u3v3 u3v4 u3v5
% u4v1 u4v2 u4v3 u4v4 u4v5
% u5v1 u5v2 u5v3 u5v4 u5v5
% F(:,2)=sums of the above columns:
% F(:,1)= x1+x1 x1+x2 x1+x3 x1+x4 x1+x5 x2+x5 x3+x5 x4+x5 x5+x5
%
%
% Notice that the first row is the vector v, scaled by u1; the second row
% is the vector v scaled by u2 and shifted one step to the right over the x
% axis; the third row is the vector v scaled by u3 and shifted 2 steps; and
% so on. Each scaled copy of v1 begins at the locus of the scaling factor
% in u.
%
% For viewing convenience, the function works with column vectors rather than
% row vectors.
%--------------------------------------------------------------------------
if (sum(A(:,2))<.99999)||(sum(A(:,2))>1.0001)||(sum(A(:,3))<.99999)||(sum(A(:,3))>1.0001)
% disp('One or both input distributions do not sum to 1')
% F=[];
% using this instead of the output above, so that we know we got an error
% when running this in excel
F = [-1];
return
end
F(:,1) = [A(1,1)+A(:,1);A(end,1)+A(2:end,1)]; % the expanded x axis
F(:,2) = conv(A(:,2),A(:,3)); % convolving the y vectors
F(:,2) = F(:,2)/sum(F(:,2)); % normalizing the convolution
%--------------------------------------------------------------------------