听起来您想使用大小根据 x 值的密度而变化的箱。我认为您仍然可以像在上一篇文章的答案中一样使用 HISTC 函数,但您只需要给它一组不同的边。
我不知道这是否正是您想要的,但这里有一个建议:不要将 x 轴分成 70 个等间距的组,而是将排序的 x 数据分成 70 个相等的组并确定边缘值。我认为这段代码应该可以工作:
% Start by assuming x and y are vectors of data:
nBins = 70;
nValues = length(x);
[xsort,index] = sort(x); % Sort x in ascending order
ysort = y(index); % Sort y the same way as x
binEdges = [xsort(1:ceil(nValues/nBins):nValues) xsort(nValues)+1];
% Bin the data and get the averages as in previous post (using ysort instead of y):
[h,whichBin] = histc(xsort,binEdges);
for i = 1:nBins
flagBinMembers = (whichBin == i);
binMembers = ysort(flagBinMembers);
binMean(i) = mean(binMembers);
end
这应该为您提供大小随数据密度而变化的箱。
更新:另一个版本...
这是我在几条评论后提出的另一个想法。使用此代码,您可以为 x 中相邻数据点之间的差异设置阈值 (maxDelta)。任何与其较大邻居相差大于或等于 maxDelta 的 x 值都将被强制放在自己的 bin 中(全部由他们的 lonsome)。您仍然可以为 nBins 选择一个值,但是当展开的点被降级到它们自己的 bin 时,最终的 bin 数量将大于此值。
% Start by assuming x and y are vectors of data:
maxDelta = 10; % Or whatever suits your data set!
nBins = 70;
nValues = length(x);
[xsort,index] = sort(x); % Sort x in ascending order
ysort = y(index); % Sort y the same way as x
% Create bin edges:
edgeIndex = false(1,nValues);
edgeIndex(1:ceil(nValues/nBins):nValues) = true;
edgeIndex = edgeIndex | ([0 diff(xsort)] >= maxDelta);
nBins = sum(edgeIndex);
binEdges = [xsort(edgeIndex) xsort(nValues)+1];
% Bin the data and get the y averages:
[h,whichBin] = histc(xsort,binEdges);
for i = 1:nBins
flagBinMembers = (whichBin == i);
binMembers = ysort(flagBinMembers);
binMean(i) = mean(binMembers);
end
我在几个小样本数据集上对此进行了测试,它似乎做了它应该做的事情。希望它也适用于您的数据集,无论它包含什么!=)