2013-02-02 183 views
0

我已經在Matlab中繪製了一個直方圖(比如一些randon數字的P(r))。現在我怎麼能得到相應於給定值r的P(r)的值?我的意思是我需要對應於給定值的直方圖的x軸在MATLABMatlab直方圖

杆高度
+0

@PeterL。我理解爲了鼓勵新用戶學習如何爲自己解決問題而做出的迴應,但要保持適當的迴應。 OP顯然不需要代碼,只需要一個簡單的答案,指出如何在文檔中找到解決方案。 – slayton

+0

@slayton謝謝,我明白了。但是你提到的OP是誰? –

+0

@PeterL。 OP->原創海報 – slayton

回答

2

Matlab documentation for hist

[n,xout] = hist(...)返回VECTORS nxout包含頻率計數和塊位置。

換句話說,hist有可選的輸出參數,其中包含您需要的信息。

1

見@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) 

enter image description here

+0

謝謝奧利查爾斯沃思。你的回答非常整潔,可以理解。做得好。 – user1281585