⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drnd.m

📁 sexy函数可以将MATLAB符号表达式变成人们书写形式的表达式。 drnd函数是我写的可以生成服从任意有限离散分布的随机数。
💻 M
字号:
function out=drnd(p,n)
%out=drnd(p,n)
%copyright:rocwoods
%p is the probability distribution matrix;
%n is the number of the samples you want to generate;
%=================
%for example
%a random variable x can be 1,2,3,4,5 with the probability 0.1 0.2 0.25 0.3 0.15(whose sum equals to 1)
%then to generate 1000 samples of x,we can make it as below:
%out=drnd([1 2 3 4 5;0.1 0.2 0.25 0.3 0.15],1000)
a=cumsum(p(2,:));
b=rand(n,1);
out=zeros(1,n);
for k=1:n
    c=find(a<b(k));
    if (isempty(c))
        out(k)=p(1,1);
    else
        out(k)=p(1,c(end)+1);
    end
end

⌨️ 快捷键说明

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