基本上,我有一个复杂的函数,我已经将其简化为一个正弦波,以便检查我的代码是否工作。我的任务是在不使用MATLAB pdf函数的情况下创建函数的pdf。我正在尝试做的是从每个数组中的最小值开始,算法在设置的步骤中逐步通过数组来创建bin,并确定数组中落入该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);有什么想法吗?谢谢!
发布于 2014-01-16 08:41:08
抱歉,伙计们,我发现了我的错误。为了让它起作用,我必须转置x。感谢您关注我的问题!
https://stackoverflow.com/questions/21150875
复制相似问题