在 Matlab 中将密度(分布)拟合到直方图

Fitting a density (distribution) to a histogram in Matlab

我有一个矩阵 M(mx2)。第一列是我的垃圾箱,第二列是与每个垃圾箱关联的频率。我想在 matlab 中为这个直方图拟合一条平滑的曲线,但是我尝试过的大部分(比如 kdensity)都需要真实的数据分布,而我没有。

是否有任何函数可以获取 bin 及其频率并为我提供平滑的 bin-freq 曲线。 ?

这里有一个适合您的 hack:从您的直方图中生成样本,然后 运行 ksdensity 在样本上。

rng(42) % seed RNG to make reproducible

% make example histogram
N = 1e3;
bins = -5:5;
counts = round(rand(size(bins))*N);
M = [bins' counts'];

figure
hold on
bar(M(:,1), M(:,2));


% draw a sample from it
sampleCell = arrayfun( @(i) repmat(M(i,1), M(i,2), 1), 1:size(M,1), 'uniformoutput', false )';
sample = cat(1, sampleCell{:});
[f, x] = ksdensity(sample);
plot(x, f*sum(M(:,2)));