对于给定的用于p=f(\theta)和2\pi之间的\theta的PMF,我在Matlab中计算了
theta=0:2*pi/n:2*pi
for i=1:n
cdf(i)=trapz(theta(1:i),p(1:i));
end并对结果进行了验证。
cumsum和cdf=cumsum(p)*(2*pi)/n进行同样的处理,但是结果是错误的。为什么?如果给定的PMF以2D形式表示为
p=f(\theta,\phi)?我能不详细解释一下here ?吗?
发布于 2022-02-06 17:01:22
在一维情况下,您可以使用cumsum获得循环的矢量化版本(假设theta和p都是列向量):
n = 10;
theta = linspace(0, 2*pi, n).';
p = rand(n,1);
cdf = [0; 0.5 * cumsum((p(1:n-1) + p(2:n)) .* diff(theta(1:n)))];在2D情况下,函数cumsum将在垂直和水平方向上应用两次:
nthet = 10;
nphi = 10;
theta = linspace(0, 2*pi, nthet).'; % as column vector
phi = linspace(0, pi, nphi); % as row vector
p = rand(nthet, nphi);
cdf1 = 0.5 * cumsum((p(1:end-1, :) + p(2:end, :)) .* diff(theta), 1);
cdf2 = 0.5 * cumsum((cdf1(:, 1:end-1) + cdf1(:, 2:end)) .* diff(phi), 2);
cdf = zeros(nthet, nphi);
cdf(2:end, 2:end) = cdf2;https://stackoverflow.com/questions/71008366
复制相似问题