简体   繁体   中英

Matlab Histogram

I have plotted a histogram (say P(r) for some randon numbers) in Matlab . How can I now get the value of P(r) corresponding to a given value of r? I mean I need the bar height corresponding to a given value on the x-axis of the histogram in MATLAB

From the Matlab documentation for hist :

[n,xout] = hist(...) returns vectors n and xout containing the frequency counts and the bin locations.

In other words, hist has optional output arguments, which contain the information you need.

See @Oli already answered this as I was creating some example code:

%# Generate random data
nPoints = 100;
data = rand(N,1);

%# Calculate histogram
[nInBin, binPos] = hist(data,20);

%#Extract P() from nInBin
P = nInBin / nPoints;

%# X position to look for histgram "height" in
posToLookFor = 0.4;

%# Find closest bin
[~, closestBin] = min(abs(binPos-posToLookFor));

%#Visualize
figure();
bar(binPos,P)
hold on;
plot([posToLookFor posToLookFor], [0 P(closestBin)],'r','linewidth',3)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM