Q:

Fit bimodal normal distribution to a vector of values, not its histogram

I would like to fit a bimodal normal distribution to data that looks bimodally distributed, such as the example below (plot(x)):

From the MATLAB docs I thought about using the mle function with a function handle to a mixture of two Gaussians:
@(x,p,mu1,mu2,sigma1,sigma2)p*normpdf(x,mu1,sigma1)+(1-p)*normpdf(x,mu2,sigma2)

However, the mle function fits to the histogram of x, while I want an approximation to x itself. How could I achieve this?

A:

The idea is to create array y whose histogram looks like your function x (which should be positive everywhere):
%// Calculate
N  = numel(x);
xi = fix(x/max(x)*100);  % Limit the memory damage
yp = arrayfun(@(n) n*ones(1,xi(n)), 1:N, 'UniformOutput', false);
y = [yp{:}];

%// Visually inspect
ax = axes('Parent', figure());
plot(ax, xi); hold(ax, 'on');
hist(ax, y, N);

