Reputation: 31
I'm trying to make a histogram of a set of data with integers from 1 to n, with one bar for each value. Setting the number of bars to be equal to the number of integers does not work:
octave:113> [nn,xx]=hist(p,max(p));
octave:114> [min(p) max(p)]
ans =
1 15
octave:115> [min(xx) max(xx)]
ans =
1.4667 14.5333
Although size(xx)
is 15 just like I want it, and sum(xx)
is the same as the sum of the unique integer values (i.e. this set of bars already has the same NORM), the values are not the integers themselves.
I understand that hist
normally handles "continuous" real data, and it is normally the edges of the bars that are important, and that this use case may not be what the function is designed for. I can get something that looks like what I want with
bar(1:max(p),histc(p,(unique(p))))
but this would fail in a very problematic way if one of the "intermediate" integers happened to appear zero times in p
.
Upvotes: 1
Views: 29
Reputation: 31
I figured this one out, and can see why others did not reply.
The answer is right in the documentation:
Given a second vector argument, x, use that as the centers of the bins, with the width of the bins determined from the adjacent values in the vector.
Replace the first command with
octave:113> [nn,xx]=hist(p,[1:max(p)]);
and the histogram generated is exactly the one I was seeking.
Upvotes: 1