声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1087|回复: 1

[编程技巧] matlab 运行时间太长了 高手解决 求缩短时间 谢谢

[复制链接]
发表于 2012-7-15 16:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
从1-8之间取1-8随机取3*10^6,总长为1.64*10^7。程序如下
clear all
c=1;
L1=8; S=1.64*10^7;N1=3*10^6;
while c>0%c是统计产生的随机数里面大于L1的个数,c=0时退出循环.初始值设为1
a=[];
a(1)=round(S*rand(1));
if(a(1)==0)
a(1)=1;
end
a(2)=S-a(1);%先将1分裂为两个随机数
while length(a)<N1
m=length(a);
[tt,ss]=sort(-a);
i=ss(1);%选择a中最大值作为分裂位置
%a(i)分裂为a(i)=q*a(i)和a(i+1)=(1-q)*a(i)
a(i+2:m+1)=a(i+1:m);%后移
q=0.2*rand(1)+0.4;
temp=a(i);
a(i)=round(a(i)*q);
if(a(i)==0)
a(i)=1;
end
a(i+1)=temp-a(i);
end
c=sum(a>L1);
end
回复
分享到:

使用道具 举报

 楼主| 发表于 2012-7-15 20:28 | 显示全部楼层
回复 1 # 可爱水若寒 的帖子

从1-8之间取1-8随机取3*10^6,总长为1.64*10^7。
clear
L=1.64*10^7;
s=3*10^6;
a=zeros(1,s);
a(1)=L;
m=1;
while sum(a>8)
n1=sum(a>8);
p=rand(1,n1);
c=a(a>8);
a(a>8)=c.*p;
a(m+1:m+n1)=c.*(1-p);
m=m+n1;
end
if m<s
n2=s-m;
id=randi(m,1,n2);
p=rand(1,n2);
a(id)=a(id).*p;
a(m+1:s)=a(id).*(1-p);
end
这个程序有问题吗  
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 03:35 , Processed in 0.063775 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表