登录论坛

查看完整版本 : Matlab函数循环


poster
2019-12-10, 16:49
我试图做一个循环来重做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(平均值)。

谢谢您的帮助


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

genSIR.m

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 主程序

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



更多&回答... (https://stackoverflow.com/questions/1894978)