卡方分布matlab代码
时间: 2023-03-27 19:04:09 浏览: 180
以下是卡方分布的 MATLAB 代码:
function [p, x2] = chi2cdf(x, v)
%CHI2CDF Chi-square cumulative distribution function (cdf).
% P = CHI2CDF(X,V) returns the chi-square cdf with V degrees of freedom
% at the values in X.
%
% The size of P is the common size of X and V. A scalar input
% functions as a constant matrix of the same size as the other input.
%
% [P,X2] = CHI2CDF(X,V) also returns the value of the chi-square
% distribution with V degrees of freedom, at the values in X.
%
% See also CHI2PDF, CHI2INV, CHI2STAT, CDF.
% References:
% [1] M. Abramowitz and I.A. Stegun, "Handbook of Mathematical
% Functions", Government Printing Office, 1964, 26.4.
% [2] L. Devroye, "Non-Uniform Random Variate Generation",
% Springer-Verlag, 1986, page 401.
if nargin < 2
error('stats:chi2cdf:TooFewInputs','Requires two input arguments.');
end
[errorcode, x, v] = distchck(2,x,v);
if errorcode >
error('stats:chi2cdf:InputSizeMismatch',...
'Requires non-scalar arguments to match in size.');
end
% Initialize P to zero.
if isa(x,'single') || isa(v,'single')
p = zeros(size(x),'single');
else
p = zeros(size(x));
end
% Return NaN for out of range parameters.
p(v <= ) = NaN;
% Compute P when X >= .
k = (x >= );
if any(k)
x = x(k);
v = v(k);
p(k) = gammainc(x/2,v/2);
end
% Compute X2.
if nargout >= 2
x2 = x;
x2(k) = v(k) + x(k);
end
end
希望这个代码能够帮到你!
阅读全文