MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   Matlab函数循环 (https://www.labfans.com/bbs/showthread.php?t=22982)

poster 2019-12-10 16:49

Matlab函数循环
 
我试图做一个循环来重做Matlab函数1000次。这是程序

d = unifrnd (0,10,[10,1]); c = d.^(-2); a = round(unifrnd(0,1,[1,10]); e = a*c btotal = e+1 SIR = 1/btotal 我想要的是将此函数迭代1000次,每次SIR的值都会由于生成的随机数而有所不同。对于每次迭代,我希望将SIR的值加在一起(加起来),然后在第1000次迭代的末尾,找到平均SIR(平均值)。

谢谢您的帮助


回答:
以下代码实现了您描述的内容:

[B] genSIR.m [/B]

function SIR = genSIR() d = unifrnd (0,10,[10,1]); c = d.^(-2); a = round(unifrnd(0,1,[1,10])); e = a*c; btotal = e+1; SIR = 1/btotal; end [B]主程序[/B]

N = 1000; SIR = zeros(N,1); for i=1:N SIR(i) = genSIR(); end s = sum(SIR) m = mean(SIR) 虽然您的功能可以简化...



[url=https://stackoverflow.com/questions/1894978]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 14:17

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.