How to construct unequal width histograms with Matlab?

落花浮王杯 提交于 2019-11-30 14:05:12

Here's an example:

x = randn(100,1)*3;           %# some random data
e = [-10 -5 -3 -1 1 2 3 20];  %# edges of intervals:  e(i) <= x < end(i+1)
c = histc(x,e);               %# get count in each interval
bar(e, c, 'histc')            %# bar plot
set(gca, 'xlim',[e(1) e(end)])

2 solutions:

  1. Specify bin centers with the 2nd argument to hist.
  2. Specify bin Edges with with the 2nd argument to histc. This function takes some further processing since it does not generate the graph directly - follow the link for a usage example.

You could build your own histogram tool to create a custom histogram as you like.

  • Use histc to determine which bins each point falls inside.

  • Then use accumarray to count the number of elements in each bin.

  • Then use bar to draw bars of your chosen widths. Or simply create patches of the given sizes. Use patch for that.

Or more simply, just use hist.

hist(rand(1000,1),[0 .1 .3 .6 1])
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!