在matlab中将一个范围划分为多个容器

时间:2022-05-04 14:56:59

I have the following range from a much larger matrix :

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

range(a)

范围(一个)

ans =

ans =

94   153   144    59    79    90   131    64

My professor is asking us to: Divide the range into N = 10 equal-length segments (hereafter called “bins”), and for each bin, find its bounds (aj, bj) as well as its center cj.

我的教授要求我们:将范围划分为N = 10个等长段(以后称为“bin”),对于每个bin,找到它的界限(aj, bj)和它的中心cj。

(5) Place each measured bacterial count xi into that bin whose lower bound is less than or equal to xi and whose upper bound is greater than xi; thereafter, for each bin count the number of xi assigned to it (= nj).

(5)将每一个被测菌数xi放入下界小于或等于xi上界大于xi的bin中;此后,对于每个bin计算分配给它的xi的个数(= nj)。

(6) Plot a histogram of the measured bacterial counts using N = 10 bars. Try the MATLAB functions hist(x,N) and bar(c, n)

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

I know this is a lot, but I have absolutely no instruction from this guy and would really appreciate a helping hand :)

我知道这很重要,但我绝对没有这个人的指导,我真的很感激你的帮助。

2 个解决方案

#1


8  

Consider the following example, it should solve all your points:

考虑下面的例子,它应该能解决你所有的问题:

%# 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')

Note that this correctly handles the last bin (read this related question for a discussion on those edge cases)

注意,这正确地处理了最后一个bin(请阅读这个相关问题,以便对这些边缘案例进行讨论)

在matlab中将一个范围划分为多个容器

#2


3  

computing histogram:

计算直方图:

range = [94   153   144    59    79    90   131    64]
[n,xout] = hist(range, 10)

xout is bin centers, n bin counts.

xout是bin centers, n bin count。

plotting bar graph:

绘制柱状图:

 bar(xout,n)

computing bin edges:

计算本边缘:

width = xout(2)-xout(1)
xoutmin = xout-width/2
xoutmax = xout+width/2

#1


8  

Consider the following example, it should solve all your points:

考虑下面的例子,它应该能解决你所有的问题:

%# 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')

Note that this correctly handles the last bin (read this related question for a discussion on those edge cases)

注意,这正确地处理了最后一个bin(请阅读这个相关问题,以便对这些边缘案例进行讨论)

在matlab中将一个范围划分为多个容器

#2


3  

computing histogram:

计算直方图:

range = [94   153   144    59    79    90   131    64]
[n,xout] = hist(range, 10)

xout is bin centers, n bin counts.

xout是bin centers, n bin count。

plotting bar graph:

绘制柱状图:

 bar(xout,n)

computing bin edges:

计算本边缘:

width = xout(2)-xout(1)
xoutmin = xout-width/2
xoutmax = xout+width/2