I have a histogram with two peaks and I want to generate the corresponding probability distribution. I have used the following MATLAB code:


I found this code frequently to explain how we can find a prob distribution for a histogram of random numbers normally distributed but I don't know whether if this true for a histogram with two peaks and generate a normalised probability distribution.

The histogram is shown here:

The bimodal histogram


2 Answers 2


You can use an kernel density eestimation. In Matlab, the function name is ksdensity. In R, the function name is density. The data used to generate the histogram are the same to generate the density.


The following should help get you started

%% Generate some data
X1 = 10 + 5 * randn(200, 1);
X2 = 20 + 8* randn(250,1);
X = [X1; X2];

%% Fit a distribution using a kernel smoother
myFit = fitdist(X, 'kernel')

%% Visualize the resulting fit
index = linspace(min(X), max(X), 1000);
plot(index, pdf(myFit, index))

%% Generate a set of 500 random numbers drawn from the distribution
numbers = random(myFit, 500, 1);

%% Inspect the complete set of methods for myFit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.