# Generate a probability distribution from a histogram with two peaks in matlab

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

A=mydata;
M1=max(A);
M2=min(A);
I=(0:100).*(M1-M2)./100+M2;
[n,x]=hist(A,I);
bar(x,n/(1000*0.352))


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:

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);
numbers(1:10)

%% Inspect the complete set of methods for myFit
methods(myFit)