2016-05-16 54 views
-3

我想創建10個大小相同的x的bin。然後將y的所有值在x的那些bin中求和。然後繪製y v/s x。我怎麼去解決它?它是否有一個功能?如何使用第1列中的分箱求和第2列中的數據?

我應該使用accumarray嗎?

x y 
0 0.0023243872 
815.54065 0.0021484715 
1111.9492 0.0023388069 
1378.9236 0.0021542402 
1631.0813 0.0021254013 
1927.4899 0.0023618778 
2194.3323 0.0021484711 
2223.8984 0.0023157364 
2446.6221 0.0022868966 
2490.8728 0.0023388073 
2743.0305 0.0024801167 
3009.7410 0.0021917303 
3262.1626 0.0022955481 
3306.2815 0.0021052146 
3335.8479 0.0023330392 
3558.5713 0.0024772326 
3602.6660 0.0023474589 
3825.1497 0.0022292205 
4121.6904 0.0021023308 
4374.1118 0.0024916520 
4447.7969 0.0023935998 
4640.5586 0.0022522912 
4714.5371 0.0023705289 
4937.0991 0.0022263369 
5233.6396 0.0021773111 
5262.8101 0.0024656970 
5455.9673 0.0024339736 
5559.7461 0.0024455092 
5752.5078 0.0021167498 
5752.5078 0.0027021724 
5826.4863 0.0023936001 
6019.4819 0.0027021721 
6048.7842 0.0021686594 
6271.3760 0.0024368572 
6345.5889 0.43 
6567.9165 0.0021167498 
6612.3291 0.0022205692 
6835.0225 0.0027165920 
7131.4312 0.0027483148 
7160.6016 0.0023849490 
7427.3418 0.0020042793 
7457.5381 0.0022032652 
7650.2212 0.0021109823 
7724.2002 0.0023301556 
7724.2783 0.0022090334 
7724.2783 0.0021801949 
7947.1040 0.0028059918 
7947.1040 0.0027425468 
8242.3545 0.0019927442 
8243.3809 0.0029588358 
8465.4980 0.0024455097 
8465.4980 0.0022032652 
8510.5107 0.0029213454 
8539.2910 0.0022148010 
8539.2910 0.0020734922 
8762.1709 0.0021686594 
8762.1709 0.0026070056 
8762.7764 0.0028232955 
8805.9531 0.0020042795 
8806.0313 0.0020590730 

回答

1

這裏的小腳本,它可以幫助你:

n = 10;%//Number of bins 
bins = linspace(0,max(x),n+1);%//Starting and ending points of your bins 
x_new = bins(1:n) + 0.5*(bins(2)-bins(1));%//Middle values of your bins 
y_new = zeros(size(x_new)); 
for k = 1:n 
    y_new(k) = sum(y((x>bins(k))&(x<bins(k+1)))); 
end 
plot(x_new,y_new) 

的關鍵是for循環中。我們使用條件索引。從矢量y中,我們只取對應於特定感興趣區域中的x值的那些值。你得到的輸出是:

enter image description here

希望幫助

+0

謝謝。是否有理由不使用hist命令來獲取bin中點? – maximusdooku

+0

@maximusdooku在這種情況下,從第一個原則開始比較容易,即使用簡單的加法找到中點 – brainkz

0

可以使用histogram功能把你的數據轉換成10個箱:

h=histogram(x,10) 

現在,你需要在每個斌總結y值。每個箱中元素的數量存儲在數組h.Values中。您可以使用for循環將相應的y -elements相加。

s=zeros(size(h.Values)); 
start=1; 
for i=1:numel(s) 
    s(i)=sum(y(start:h.Values(i)+start-1)); 
    start=h.Values(i)+start; 
end 

現在你可以繪製。這裏每個值都是相對於相應的倉中心繪製的。

plot(linspace(h.BinLimits(1)+h.BinWidth/2,h.BinLimits(2)-h.BinWidth/2,h.NumBins),s); 
相關問題