简体   繁体   English

Matlab直方图

[英]Matlab Histogram

I have plotted a histogram (say P(r) for some randon numbers) in Matlab . 我在Matlab中绘制了一个直方图(对某些randon数说P(r))。 How can I now get the value of P(r) corresponding to a given value of r? 现在如何获得与给定r值相对应的P(r)值? I mean I need the bar height corresponding to a given value on the x-axis of the histogram in MATLAB 我的意思是我需要与MATLAB中直方图的x轴上给定值相对应的条形高度

From the Matlab documentation for hist : histMatlab文档中

[n,xout] = hist(...) returns vectors n and xout containing the frequency counts and the bin locations. [n,xout] = hist(...)返回包含频率计数和bin位置的向量nxout

In other words, hist has optional output arguments, which contain the information you need. 换句话说, hist具有可选的输出参数,其中包含您需要的信息。

See @Oli already answered this as I was creating some example code: 看到@Oli在创建示例代码时已经回答了这个问题:

%# 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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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