it's not clear what's your variable, but i'm assuming it's r
.
the simplest way to do this is, as Chris noted, first get the cdf (note that if r starts at 0, pdf(1)
is Nan... change it to 0
):
cdf = cumtrapz(pdf);
cdf = cdf / cdf(end);
then spawn a uniform distribution (size_dist indicating the number of elements):
y = rand (size_dist,1);
followed by a method to place distribution along the cdf. Any technique will work, but here is the simplest (albeit inelegant)
x = zeros(size_dist,1);
for i = 1:size_dist
x(i) = find( y(i)<= cdf,1);
end
and finally, returning to the original pdf. Use matlab numerical indexing to reverse course. Note: use r
and not pdf
:
pdfHist = r(x);
hist (pdfHist,50)