MATLAB - 使用bin创建正弦波PDF

时间:2014-01-16 00:01:50

标签: matlab

基本上,我有一个复杂的功能,我已经简化为一个sin波,以检查我的代码是否正常工作。我的任务是在不使用MATLAB pdf函数的情况下创建函数的pdf。我想要做的是从每个数组中的最小值开始,算法在设置步骤中逐步通过数组来创建容器并确定数组中属于该bin的值的数量。我尝试使用在线找到的示例有道理,但似乎并没有为我工作。这是我的代码:

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);

任何想法?谢谢!

1 个答案:

答案 0 :(得分:0)

对不起家伙我发现了我的错误。我必须转换x才能使它工作。感谢您查看我的问题!