randpdf2(x, y, p, m)

版本 1.0.0.0 (2.5 KB) 作者: Avi Silbiger
Draw random numbers from a 2-dimensional user-defined pdf
37.0 次下载
更新时间 2017/12/11

查看许可证

function z=randpdf2(x, y, p, m)
% RANDPDF2
% Random numbers from a user defined 2-dimensional distribution
%
% SYNTAX:
% z = randpdf2(x, y, p, n)
% randpdf2(x, y, p, n)
%
% INPUT:
% x - x random variable - first dimension (1 x k) vector
% y - y random variable - second dimension (1 x k) vector
% p - probability density (1 x k) vector
% m - length of the output vector (scalar)
%
% OUTPUT:
% z - A (2 x n) matrix of random pairs (x,y).
% Run function without output for some plots.
%
% DESCRIPTION:
% z=randpdf2(x, y, p, n) returns the matrix of random numbers from
% probability density distribution defined by (x,y) and p. p is a 1 x k
% vector which is the probability density (pdf).
% x, y, and p must be of the same length.
% p must be non-negative (as a valid pdf). Its integral does not have to
% be 1, as it is normalized by randsample().
% m defines the output matrix length. Hence, the output is a 2 x n matrix
%
%
%
% EXAMPLE-1:
% Generation of random numbers from the distribution: f(x,y)=x+y for 0<=x,y<=1
% When one computes the correlation between x and y, one gets -1/11
%
% x=0:1e-2:1;
% y=x;
% [X Y]=meshgrid(x,y);
% X=X(:); % column vectors
% Y=Y(:);
% p=X+Y; % pdf
% X=X'; % row vectors
% Y=Y';
% p=p';
% Z=randpdf2(X,Y,p,100000);
% corr(Z')
% plot a 3D histogram
% hist3(Z')
% xlabel('X')
% ylabel('Y')
%
% EXAMPLE-2:
% A discrete distribution. x=-1 or 1; y=-1, 0, or 1 with the following
% probabilities:
%
% | -1 | 1 |
% -----------------
% -1| 1/8 | 1/8 |
% -----------------
% 0| 1/8 | 1/8 |
% -----------------
% 1| 2/8 | 2/8 |
%
% x=[-1 1];
% y=[-1 0 1];
% [X Y]=meshgrid(x,y);
% X=X(:);
% Y=Y(:);
% p=[1/8 1/8 2/8 1/8 1/8 2/8];
% X=X';
% Y=Y';
% Z=randpdf2(X,Y,p,100000);
%
% plot a 3D histogram
% ctrs={[-1 1]; [-1 0 1]};
% hist3(Z',ctrs)
% xlabel('X')
% ylabel('Y')

引用格式

Avi Silbiger (2024). randpdf2(x, y, p, m) (https://www.mathworks.com/matlabcentral/fileexchange/65387-randpdf2-x-y-p-m), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2015a
兼容任何版本
平台兼容性
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0