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
0
votes
@PeterL. I understand the response to encourage new users to learn how to solve problems for themselves, but keep the responses appropriate. The OP isn't obviously asking for code, a simple answer indicating how to find the solution in the docs is all that is needed.
- slayton
@slayton thanks, I got the idea. But who is OP you mention?
- Peter L.
@PeterL. OP-> Original poster
- slayton
@slayton thanks, was not familiar with such acronym.
- Peter L.
2 Answers
2
votes
From the Matlab documentation for hist:
[n,xout] = hist(...)returns vectorsnandxoutcontaining the frequency counts and the bin locations.
In other words, hist has optional output arguments, which contain the information you need.
1
votes
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)
