tl;博士:
x = [ 0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595 ]
[ ~, Bins ] = histc( x, 0: 0.25: 1 )
% Bins = 1 4 4 2 4 1 2 4 4 4
说明:
根据matlab手册:
早期版本的 MATLAB® 使用 hist 和 histc 函数作为创建直方图和计算直方图 bin 计数的主要方法 [...] 不鼓励在新代码中使用 hist 和 histc [... ] histogram、histcounts 和 discretize 是推荐用于新代码的直方图创建和计算函数。
和
discretize 的行为类似于 histcounts 函数的行为。使用 histcounts 查找每个 bin 中的元素数。另一方面,使用 discretize 来查找每个元素属于哪个 bin(不计算)。
Octave 还没有实现discretize,但仍然支持histc,正如上面所暗示的,它做同样的事情,但界面不同。
根据histc的octave文档
-- [N, IDX] = histc ( X, EDGES )
Compute histogram counts.
[...]
When a second output argument is requested an index matrix is also
returned. The IDX matrix has the same size as X. Each element of
IDX contains the index of the histogram bin in which the
corresponding element of X was counted.
因此你的问题的答案是
[ ~, Bins ] = histc( x, 0:0.25:1 )
使用您的示例:
x = [ 0.1576, 0.9706, 0.9572, 0.4854, 0.8003, 0.1419, 0.4218, 0.9157, 0.7922, 0.9595 ]
[ ~, Bins ] = histc( x, 0: 0.25: 1 )
% Bins = 1 4 4 2 4 1 2 4 4 4
PS。如果你喜欢discretize提供的接口,你可以很容易地自己创建这个函数,通过适当地包装histc:
discretize = @(X, EDGES) nthargout( 2, @histc, X, EDGES )
您现在可以像在您的示例中一样直接使用此 discretize 函数。