sample_beta.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 77 行

M
77
字号
function r = betarnd(a,b,m,n);%BETARND Random matrices from beta distribution.%   R = BETARND(A,B) returns a matrix of random numbers chosen   %   from the beta distribution with parameters A and B.%   The size of R is the common size of A and B if both are matrices.%   If either parameter is a scalar, the size of R is the size of the other%   parameter. Alternatively, R = BETARND(A,B,M,N) returns an M by N matrix. %   Reference:%      [1]  L. Devroye, "Non-Uniform Random Variate Generation", %      Springer-Verlag, 1986%   Copyright (c) 1993-98 by The MathWorks, Inc.%   $Revision: 2.5 $  $Date: 1997/11/29 01:44:53 $if nargin < 2,     error('Requires at least two input arguments'); end if nargin == 2    [errorcode rows columns] = rndcheck(2,2,a,b);endif nargin == 3    [errorcode rows columns] = rndcheck(3,2,a,b,m);endif nargin == 4    [errorcode rows columns] = rndcheck(4,2,a,b,m,n);endif errorcode > 0    error('Size information is inconsistent.');endr = zeros(rows,columns);% Use Theorem 4.1, case A (Devroye, page 430) to derive beta%   random numbers as a ratio of gamma random numbers.if prod(size(a)) == 1    a1 = a(ones(rows,1),ones(columns,1));    g1 = gamrnd(a1,1);else    g1 = gamrnd(a,1);endif prod(size(b)) == 1    b1 = b(ones(rows,1),ones(columns,1));    g2 = gamrnd(b1,1);else    g2 = gamrnd(b,1);endr = g1 ./ (g1 + g2);% Return NaN if b is not positive.if any(any(b <= 0));    if prod(size(b) == 1)        tmp = NaN;        r = tmp(ones(rows,columns));    else        k = find(b <= 0);        tmp = NaN;        r(k) = tmp(ones(size(k)));    endend% Return NaN if a is not positive.if any(any(a <= 0));    if prod(size(a) == 1)        tmp = NaN;        r = tmp(ones(rows,columns));    else        k = find(a <= 0);        tmp = NaN;        r(k) = tmp(ones(size(k)));    endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?