function xy = discrete_cdf_to_xy ( n1, n2, cdf, n, u )
%*****************************************************************************80
%
%% discrete_cdf_to_xy() finds XY points corresponding to discrete CDF values.
%
% Discussion:
%
% This program is given a discrete CDF function and a set of N random
% values U. Each value of U corresponds to a particular (I,J) subregion
% whose CDF value just exceeds the value of U. Inside that subregion,
% we pick a point at random - this is equivalent to assuming the PDF
% is constant over the subregion.
%
% This function is part of an example program, for which various
% assumptions have been made. In particular, the region is the unit
% square, and the subregions are formed by a 20 by 20 grid of equal
% subsquares.
%
% Licensing:
%
% This code is distributed under the MIT license.
%
% Modified:
%
% 03 January 2012
%
% Author:
%
% John Burkardt
%
% Input:
%
% integer N1, N2, the number of rows and columns
% of PDF data.
%
% real CDF(N1,N2), the CDF values associated with each subcell.
% A particular ordering has been given to the subcells so that the
% CDF is a monotonoe function when the subcells are listed in that order.
%
% integer N, the number of sample points.
%
% real U(N), N random values.
%
% Output:
%
% real XY(2,N), the sample points.
%
xy(1:2,1:n) = 0.0;
low = 0.0;
for j = 1 : n2
for i = 1 : n1
high = cdf(i,j);
for k = 1 : n
if ( low <= u(k) & u(k) <= high )
r = rand ( 2, 1 );
xy(1,k) = ( ( i - 1 ) + r(1) ) / n1;
xy(2,k) = ( ( j - 1 ) + r(2) ) / n2;
end
end
low = high;
end
end
return
end