我试图通过在MATLAB中自己实现它而不是使用内置的raylpdf
函数来可视化瑞利分布的概率分布函数。
Rayleigh发行的PDF:
这是我的尝试:
function pdf = rayleigh_pdf(x)
exp_term = -1*power(x,2)/(2*std(x))
pdf = (x*exp(exp_term))/std(x)
end
但是当我尝试运行它时,出现错误:
x = linspace(-10,10,100);
plot(x,rayleigh_pdf(x))
错误:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.
我不确定为什么会收到此错误。我应该在哪里使用.*
?为何需要它?
dot-before-operator允许一个人执行element-wise操作,而不是默认的matrix操作。如果您编写的代码没有点,那么很有可能会遇到尺寸错误(例如,由于您尝试使用不匹配的尺寸进行矩阵乘法),或者由于自动广播而获得非常奇怪的结果,您最终得到的尺寸超出了您的预期。
function pdf = rayleigh_pdf(x)
exp_term = -x.^2./(2.*std(x).^2);
pdf = (x.*exp(exp_term))./(std(x).^2)
end
旁注:sigma平方通常表示方差,即标准差平方。因此,请使用std(x).^2
或var(x)
。
请注意,某些点是多余的,例如当您确定要处理整数时(在MATLAB中也称为1 -by- 1个矩阵)。因此,您可以编写等效的以下内容:
var(x)
即仅当在数组function pdf = rayleigh_pdf(x)
exp_term = -x.^2/(2*std(x)^2);
pdf = (x.*exp(exp_term))/(std(x)^2)
end
和x
上工作时才需要点,而在exp_term
或2
等标量上则不需要。