注册地址 登录
数学建模社区-数学中国 返回首页

intenvec的个人空间 http://www.madio.net/?291918 [收藏] [复制] [分享] [RSS]

日志

MATLAB的经典算法

已有 292 次阅读2011-8-10 22:05

function r=specrnd(x,p,m,n)
% 生成给定分布律的随机数
%    r=specrnd(x,p)返回一个来自分布律P(x)=p的随机数
%    r=specrnd(x,p,m,n)返回m*n随机数矩阵
%    p的默认值为等概率
% 例如 分布律
%       ξ     7      8      9     10
%        p     0.1   0.2   0.4    0.3
%  命令    specrnd([7 8 9 10],[0.1 0.2 0.4 0.3],10,10)
%  产生100个模拟数据  
    
% L. J. HU 9-10-1999

if nargin<4, n=1;end
if nargin<3, m=1;end
if nargin<2|isempty(p), p=1/length(x)*ones(size(x)); end;

[mx,nx]=size(x);[mp,np]=size(p);
sx=min(mx,nx);sp=min(mp,np);
if (mx~=mp)|(nx~=np),
   error('size must agree');
end;
if (sx~=1)&(sx~=1),error('should be vector'); end;
x=x(:);
if any(x~=sort(x)), error('x should be acsend'); end;
if any((p<0)|(p>1)),error('p should be between 0 and 1'); end;
if abs(sum(p)-1)>1e-3,error('sum of p should be 1'); end;

r=zeros(m,n);lx=max(mx,nx);
s=rand(m,n);
cp=cumsum(p);
k=find(s<cp(1));r(k)=x(1);
k=find(s>=cp(lx-1));r(k)=x(lx);
for i=2:(lx-1),
   k=find((s>=cp(i-1))&(s<cp(i)));
   r(k)=x(i);
end


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-10 01:43 , Processed in 0.187073 second(s), 28 queries .

回顶部