考虑以下示例,它应该解决您的所有要点:
%# random data vector of integers
M = randi([50 200], [100 1]);
%# compute bins
nbins = 10;
binEdges = linspace(min(M),max(M),nbins+1);
aj = binEdges(1:end-1); %# bins lower edge
bj = binEdges(2:end); %# bins upper edge
cj = ( aj + bj ) ./ 2; %# bins center
%# assign values to bins
[~,binIdx] = histc(M, [binEdges(1:end-1) Inf]);
%# count number of values in each bin
nj = accumarray(binIdx, 1, [nbins 1], @sum);
%# plot histogram
bar(cj,nj,'hist')
set(gca, 'XTick',binEdges, 'XLim',[binEdges(1) binEdges(end)])
xlabel('Bins'), ylabel('Counts'), title('histogram of measured bacterial')请注意,这会正确处理最后一个bin(请阅读此related question以获取有关这些边缘情况的讨论)