Search code examples
matlabhistogramcurve

Matlab Histogram of randomly spread data


I am a newbie to matlab and I already search a whole bunch of different sites but I didn't quite find a solution (at least an understandable one).

I have a matrix 290x233 with double values ranging from 0.1 to 4.7. I want to generate a histogram out of all the data in this matrix in way that there are bars for a range of 0.5 -> I want to be able to see how many values there are within 0 to 0.5, from 0.5 to 1 and so on ...

So far I managed to get kind of close plotting the bars. Using this code:

bins=[0.25:0.5:4.75];
n2=histc(a_dif_1, bins);
bar(bins,n2,'hist');

I managed to get a result where there are a whole bunch of peaks within each bin (see image below). This means it shows me the occurance of each single value within the bin range.

enter image description here

then I found out about the "stacked" option and using

bins=[0.25:0.5:4.75];
n2=histc(a_dif_1, bins);
bar(bins,n2,0.8,'stacked','b');

This way I at least got big bars just showing the TOTAL number of occurances within 1 bin (therefor 10 rectangleshaped bars shown below).

enter image description here

Right now I am wondering if this is the right way to draw bars just showing the total amount of values within a bin?? Apart of this I noticed that my bars I kind of getting set off the center of the bin (the bars to the right side are not axactly in the middle of two x-ticks) - did I mess something up with the bins??

As kind of a bonus I finally would like to draw a curve on top of the bars. The curve should show follow the peaks I described before in a very generalized way so I can give a more detailed information about the distribution of the data apart of the bars.

Thanks for every kind of help or ideas!

UPDATE

THANK YOU VERY MUCH for the hints. I changed the histc to hist

bins=[0.25:0.5:4.75]; 
hist(a_dif_1(:), bins)

...and Matlab is giving me exactly what I wanted:

enter image description here


Solution

  • a = 0.1;
    b = 4.7;
    r = a + (b-a).*rand(290,233);
    bins=[0.25:0.5:4.75];
    hist(r(:))
    hist(r(:),bins)
    

    is that ok for you?

    enter image description here