Matlab为一个函数循环 [英] Matlab loops for a function

查看:441
本文介绍了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(平均值)。

感谢您的帮助

解决方案

下面的代码实现了您所描述的内容:

genSIR.m



 函数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



< h3>主程序

  N = 1000; 
SIR =零(N,1);
i = 1:N
SIR(i)= genSIR();
end
s = sum(SIR)
m = mean(SIR)

尽管您的功能可以简化...


I am trying to make a loop to redo a Matlab function 1000 times. Here's the program

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

What I want is to iterate this function 1000 times, each time the value of SIR will vary due to the random number generated. For every iteration, I want the value of SIR to be added together (summed up), and in the end of the 1000th iteration, find the average SIR(mean).

Thanks for the help

解决方案

The code below implements what you described:

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

main program

N = 1000;
SIR = zeros(N,1);
for i=1:N
    SIR(i) = genSIR();
end
s = sum(SIR)
m = mean(SIR)

although your function could be simplified...

这篇关于Matlab为一个函数循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆