2
votes

I'm trying to have the data be the x-axis and the y-axis be the amount of values grouped by 10. See Plot below as example

I tried using bar(x) but I'm not sure how you get the numbers in the correct categories for plotting.

example: if the data is x=1,5.3,9,10.5,12,13,15.2,25,191,192.4

the group 0-10 should be 1,5.3,9
the group 10.1-20 should be 10.5,12,13,15.2
the group of 20.1-30 should be 25
.
.
.
the group of 190.1-200 should be 191,192.4

Barplot

PS: I'm using octave 3.8.1 which is like matlab

2
Are you looking for a histogram? mathworks.com/help/matlab/ref/hist.html - Daniel
@Daniel thanks I just couldn't remember the name of it - Rick T

2 Answers

3
votes

You could use histc. But histc considers equality at the left edge of each bin, not at its right edge:

bincounts = histc(x,binranges) counts the number of values in x that are within each specified bin range. The input, binranges, determines the endpoints for each bin. The output, bincounts, contains the number of elements from x in each bin.

For example, if binranges equals the vector [0,5,10,13], then histc creates four bins. The first bin includes values greater than or equal to 0 and strictly less than 5. The second bin includes values greater than or equal to 5 and less than 10, and so on. The last bin contains the scalar value 13.

To set the equality condition at the left edges, it's better to do it manually with bsxfun:

y = diff(sum(bsxfun(@le, x(:), 0:10:200), 1));

sum(bsxfun(...), 1) finds how many entries of x are smaller or equal to 0, 10, ... 200; and then diff(...) gives the desired result y:

y =
     3     4     1     0     0     0     0     [...]     2

You can then use bar(y) to plot the bars. If you additionally want to change the texts shown on the x-axis, set the 'xticklabel' property of the axes:

bar(y);
strings = {'0-10', '10-20', '20-30'}; %// manually define all strings up to '190-200'
set(gca, 'xticklabel', strings)
1
votes

With your data, you can use:

x = [1 5.3 9 10.5 12 13 15.2 25 191 192.4];
nbins = round(max(x)/10+.5);
hist(x, nbins);

This is the result: This is the result