Search code examples
matlabhistogramnormalize

Matlab - multiple variables normalized histogram?


I'm working on MATLAB, where I have a vector which I need to split into two classes and then get a histogram of both resulting vectors (which have different sizes). The values represent height records so the interval is about 140-185. How can I get a normalized histogram of both resulting vectors in different colors. I was able to get both normalized vectors in the same colour (which is indistiguible) and and also a histogram with different colours but not not normalized...

I hope you understand my question and will be able to help me. Thanks in advance :)


Solution

  • Maybe this is what you need:

    matrix = [155+10*randn(2000,1) 165+10*randn(2000,1)];
    matrix(1:1100,1) = NaN;
    matrix(1101:2000,2) = NaN;  %// example data
    
    [y x] = hist(matrix, 15); %// 15 is desired number of bins
    y = bsxfun(@rdivide, y, sum(y)) / (x(2)-x(1)); %// normalize to area 1
    bar(x,y) %// plots each column of y vs x. Automatically uses different colors
    

    enter image description here