How to construct unequal width histograms with Matlab?

12,852

Solution 1

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)])

output

Solution 2

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.
Share:
12,852
yasmine
Author by

yasmine

Updated on June 14, 2022

Comments

  • yasmine
    yasmine almost 2 years

    I would like to construct a histogram with unequal bins (intervals)..Matlab construct only histograms with equal bins as if it's a diagram..!!!

    Please help me...thanks a lot!!