2014-01-16 26 views
0

基本上,我有一個複雜的功能,我簡化爲正弦波以檢查我的代碼是否正常工作。我的任務是在不使用MATLAB pdf函數的情況下創建函數的pdf。我想要做的是從每個數組中的最小值開始,算法通過設置步驟中的數組來逐步創建箱並確定數組中的值落入該箱中。我嘗試使用我在網上找到的示例有道理,但似乎並沒有爲我工作。這裏是我的代碼:MATLAB - 使用容器創建正弦波的PDF

clear all 
clc 
A = 1; 
E = 1; 
a1 = 0; 
a2 = 0; 
a3 = 0; 
w = pi; 
y = 0; 
% ts = .1; 
% t = 0:ts:10; 
t = -1:0.01:1; 
x = A*(1+a1*E)*sin(w*(1+a2*E)*t+y)+ a3*E; 

%# compute bins 
nbins = length(x); 
binEdges = linspace(min(x),max(x),nbins+1); 
aj = binEdges(1:end-1);  %# bins lower edge 
bj = binEdges(2:end);  %# bins upper edge 
cj = (aj + bj) ./ 2;  %# bins center 

%# assign values to bins 
[~,binIdx] = histc(x, [binEdges(1:end-1) Inf]); 

%# count number of values in each bin 
nj = accumarray(binIdx, 1, [nbins 1], @sum); 

%# plot histogram 
bar(cj,nj,'hist') 
set(gca, 'XTick',binEdges, 'XLim',[binEdges(1) binEdges(end)]) 
xlabel('Bins'), ylabel('Counts'), title('PDF') 

,這是錯誤我得到:

Error using accumarray 
Third input SZ must be a full row vector with one element for each 
column of SUBS. 

Error in Try1 (line 40) 
nj = accumarray(binIdx, 1, [nbins 1], @sum); 

任何想法?謝謝!

回答

0

對不起,我發現我的錯誤。爲了使它工作,我必須轉置x。感謝您看我的問題!