我有一个像这样的矩阵:
A=
10 31 32 22
32 35 52 77
68 42 84 32
我需要一个类似 mode
但有范围的函数,例如返回 30 的 mymode(A,10),在 0-10、10-20、20-30 范围内找到最频繁出现的数字, .... 并返回范围内的最多数字。
最佳答案
您可以使用 histc
将数据分箱到您想要的范围内,然后使用 max
找到成员最多的分箱关于 histc
ranges = 0:10:50; % your desired ranges
[n, bins] = histc(A(:), ranges); % bin the data
[v,i] = max(n); % find the bin with most occurrences
[ranges(i) ranges(i+1)] % edges of the most frequent bin
对于您的具体示例,这将返回
ans =
30 40
这与您所需的输出相匹配,因为 A 中的大多数值在 30 到 40 之间。
关于Matlab:在范围内查找模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12419597/