【问题标题】:Using matlab to create and graph distributions使用 matlab 创建和绘制分布图
【发布时间】:2018-07-13 05:49:05
【问题描述】:

如何将代码写入六面骰子,然后将显示的两个数字相加以产生 2 到 12 之间的总和?然后绘制它

【问题讨论】:

  • 尝试过什么吗?如果你还没有,我建议你先试试。如果有,请张贴您写的任何内容,并告诉我们困难是什么。

标签: matlab normal-distribution uniform-distribution


【解决方案1】:

此代码用于 10 次观察。您可以根据自己的条件进行更改。

for i =1:1:10
   first_no = randi([1 6],1);
   second_no = randi([1 6],1);
   if second_no == first_no
      second_no = randi([1 6],1);
   end
   sum(i) = first_no + second_no
   no(i) = 1
end
figure;
plot(no, sum)

【讨论】:

    【解决方案2】:

    您只需要定义两个变量,取 1-6 之间的随机值。一开始,您可以选择观察次数。请看下面的代码:

       no_obs = 5;
       for  i=1:no_obs
       num1 = randi([1 6],1);
       num2 = randi([1 6],1);
       sum(i) = num1 + num2;
       end
       display(sum);
       figure;
       plot(sum)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-07
      • 2017-08-07
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多