Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

plot histogram without using matlab hist() function

Asked by ws on 22 Dec 2011

Hi all

There is a function called hist(), but if I want to plot a graph without using hist() command, how to plot a graph? I means using command to do the job same as hist().

Thank you.





No products are associated with this question.

1 Answer

Answer by Naz on 22 Dec 2011
Accepted answer

It depends on how you want to bin your data and what kind of data you have.

The idea of hist function is similar to stacks of coins: Let's say you have a bag of coins and after you separate them by values and stack on one another, you will get different heights. The hist function does the same.

data=[1 3 5 7 4 8 0 1 3];

If you want to distribute your data into 10 bins, you would create a new array of size 10:

histArray=zeros(1,10);  % prealocate
then, you would run a forloop to count how many times you encounter in a particular value:
for n=1:length(data)
    histArray(1,data(n)+1)=histArray(1,data(n)+1)+1; % every time you meet the particular value, you add 1 into to corresponding bin

There could be another case. Let's say you want to distribute your data into 5 bins only (0 and 1 go in the 1st bin, 2 and 3 go in the 2nd...). In this case, you would run the same loop but with more complicated conditioning:

    histArray=zeros(1,5);  %prealocate
    x=0:2:8  % create bin numbers for plot
    for n=1:length(data)
        % every time you meet the particular value
        %you add 1 into to corresponding bin

You can design your own hist function to fit you needs

1 Comment

ws on 22 Dec 2011

thanks, this is what i'm looking for.. :)


Contact us