在 Matlab 中等间距 "histogram"

equally spaced "histogram" in Matlab

我有一个数字向量,假设 v=[1 1 1 1000 20 20];我想构建非常简单的类似直方图的图,其中 y 轴将被计数(在本例中为 3,1,2),但 x 轴将等于 1,20,1000 spaced。这意味着 Matlab 将忽略 1000>>20 和 space 它们的事实,因为它将在图中显示 1,2,3。 我会证明我的意思。我可以通过:

x=[1 1 1 1000 20 20];
histogram('Categories',{'1','20','1000'},'BinCounts',[numel(find(x==1)), numel(find(x==20)), numel(find(x==1000))])

但这是一种笨拙笨拙的方式。我必须事先知道类别。可以用更优雅的方式来完成吗?

首先将 X 转换为分类矩阵 convert to categorical 然后使用这些类别对 X 进行直方图绘制: plot categorial histogram

如果您遇到问题:

C = categorical(x,[1 20 1000],{'1','20','1000'})
//or just C=categorical(x,[1 20 1000]) or simply C=categorical(x)
histogram(C)