creating a histogram in a specific way

4 views (last 30 days)
hi, I want to make a histogram that gets a vector with values from the group (0 1 10 100, and creates a histogram that has one bin for each of those values and has the axis of x also sorted only for those values. I tried few things and nothing seems to work. I wand the bins in the same width.

Accepted Answer

the cyclist
the cyclist on 14 Dec 2011
r = [0 0.1 0.2 0.3 1 2 3 10 20 30 40 50 60 70 80 90];
bins = [0 1 10 100];
count=hist(r,bins)
figure
bar(bins,count)
set(gca,'XTick',bins)
OR
r = [0 0.1 0.2 0.3 1 2 3 10 20 30 40 50 60 70 80 90];
bins = [0 1 10 100]
count=hist(r,bins)
figure
bar(1:4,count)
set(gca,'XTickLabel',bins)
  1 Comment
the cyclist
the cyclist on 14 Dec 2011
If you use either of these solutions, do be aware of the issue that Sean brings up, which is whether you want the binning itself to be on a log scale. The binning in my solution is on a linear scale (even in the solution in which I display the results on what is essentially a log scale).

Sign in to comment.

More Answers (1)

Sean
Sean on 14 Dec 2011
Are you trying to bin on a log scale or are you trying to bin to arbitrary values?
If you are trying to do the first, you might try taking the log10() of all your input values and binning them linearly (i.e. less than 1 is negative, 1-9.9999 is is 0-1, 10-99.9999 is 1-2, etc.) You could round the values to fall into whichever bin you wanted.
  1 Comment
Dr. Seis
Dr. Seis on 14 Dec 2011
Agreed... and then holding the bin "width" the same would make more sense.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!