在matlab中将范围划分为bin

pat*_*lon 2 matlab range histogram bins

我有一个更大矩阵的以下范围:

范围(a)中

ans =

94   153   144    59    79    90   131    64
Run Code Online (Sandbox Code Playgroud)

我的教授要求我们:将范围划分为N = 10个等长段(以下称为"箱"),并且对于每个箱,找到它的边界(aj,bj)以及它的中心cj.

(5)将每个测量的细菌数xi放入下限小于或等于xi且上限大于xi的bin中; 此后,对于每个bin计数,分配给它的xi的数量(= nj).

(6)使用N = 10巴绘制测量的细菌计数的直方图.尝试MATLAB函数hist(x,N)和bar(c,n)

我知道这很多,但我绝对没有这个家伙的指示,真的很感激帮助:)

Amr*_*mro 8

考虑以下示例,它应该解决您的所有要点:

%# 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')
Run Code Online (Sandbox Code Playgroud)

请注意,这正确处理最后一个bin(阅读此相关问题以讨论这些边缘情况)

截图