Matlab - multiple variables normalized histogram?

5 views (last 30 days)
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 :)

Answers (3)

Kerem  tezcan
Kerem tezcan on 2 May 2014
Edited: Kerem tezcan on 2 May 2014
Hello, I have generated a random vector to have some data and classified them as lower or higher than a threshold (170) since I don't know how you want to do the classification. But the histograms you can get as follows:
>>
>> v=140+round(50*rand(100,1));
>> v1=v(v<170);
>> v2=v(v>=170);
>> n_bins=30; %no of bins you want for the histograms
>> bins=140:50/n_bins:190;%create the bins
>> [h1 b1]=hist(v1, bins);%make the histograms, the values are in h1 and h2
>> [h2 b2]=hist(v2, bins);
>> bar(bins, h1,'r'); hold on; bar(bins, h2,'b');%not notmalized histograms
>> bar(bins, h1/length(v1),'r'); hold on; bar(bins, h2/length(v2),'b');%normalized histograms
Of course you need to change the lower and upper limits, I have taken here 140 to 190... You can change the colors as well. Don't forget to write hold off after you are done with plotting...

Star Strider
Star Strider on 2 May 2014
Another approach:
fh = 163+3.5*randn(1,45); % Original data female (45 data)
mh = 175+3.5*randn(1,55); % Original data male (55 data)
bins = linspace(150,200, 25); % Choose 25 bins
fc = histc(fh,bins); % Histogram count for females
fcn = fc/max(fc); % Normalised histogram count for females
mc = histc(mh,bins); % Histogram count for males
mcn = mc/max(mc); % Normalised histogram count for males
figure(1)
bar(bins',[fcn' mcn']) % Plot
axis([140 200 0 1])

Daniel Bridges
Daniel Bridges on 4 Apr 2016
MATLAB R2015b has normalization built-in:
help histogram
This command brings up the following documentation, indicating you can specify normalization as a command property, without any additional commands:
histogram(...,'Normalization',NM) specifies the normalization scheme
of the histogram values. The normalization scheme affects the scaling
of the histogram along the vertical axis (or horizontal axis if
Orientation is 'horizontal'). NM can be:
'count' The height of each bar is the number of
observations in each bin, and the sum of the
bar heights is NUMEL(X).
'probability' The height of each bar is the relative
number of observations (number of observations
in bin / total number of observations), and
the sum of the bar heights is 1.
'countdensity' The height of each bar is the number of
observations in each bin / width of bin. The
area (height * width) of each bar is the number
of observations in the bin, and the sum of
the bar areas is NUMEL(X).
'pdf' Probability density function estimate. The height
of each bar is, (number of observations in bin)
/ (total number of observations * width of bin).
The area of each bar is the relative number of
observations, and the sum of the bar areas is 1.
'cumcount' The height of each bar is the cumulative
number of observations in each bin and all
previous bins. The height of the last bar
is NUMEL(X).
'cdf' Cumulative density function estimate. The height
of each bar is the cumulative relative number
of observations in each bin and all previous bins.
The height of the last bar is 1.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!