我无法使用数组
在Matlab中生成直方图% initialising the five arrays to hold the averages of five probabilities of interests
ar1=zeros(1,100);
ar2=zeros(1,100);
ar3=zeros(1,100);
ar4=zeros(1,100);
ar5=zeros(1,100);
%initialising the variable to count the number of experiments
k=1;
while k<=100,
%generating the required random numbers for the proble
%pi is the probablity in winning the ith game
p1=rand(1);
p2=rand(1)*p1;
p3=rand(1)*p2;
p4=rand(1)*p3;
%initialising variable to count the number of tournaments
count_tour=1;
%initialising the variables in order to get the sum of all probabilties of interests and then we can get our respective averages
t1=0; t2=0; t3=0; t4=0; t5=0;
%starting the loop for 50 tournaments
while count_tour<=50,
%Total probabilties of winning the ith game
W1=p1;
W2=p1*(1+p2-p1);
W3=(p1*p2*p3)+((p1*p1)*(2-p1-p2))+((p4)*(1-p1)*(1-p1));
%probabilty that player had won the first game given that he won the second game
W4=(p1*p2)/W2;
%probabilty of winning all three games
W5=p1*p2*p3;
%getting the sum of all probabilies in 50 tournaments
t1=t1+W1;
t2=t2+W2;
t3=t3+W3;
t4=t4+W4;
t5=t5+W5;
count_tour=count_tour+1;
end
%getting the averages of probabilties of interest in 50 tournaments
av1=t1/50;
av2=t2/50;
av3=t3/50;
av4=t4/50;
av5=t5/50;
ar1(k)=ar1(k)+av1;
ar2(k)=ar2(k)+av2;
ar3(k)=ar3(k)+av3;
ar4(k)=ar4(k)+av4;
ar5(k)=ar5(k)+av5;
k=k+1;
end
figure();
h1=histogram(ar1);
h2=histogram(ar2);
h3=histogram(ar3);
h4=histogram(ar4);
h5=histogram(ar5);
答案 0 :(得分:1)
假设计算数组ar1, ar2, ar3, ar4, ar5
的部分是正确的,并且还考虑了@EBH答案中提出的更新,问题可能在于您绘制直方图的方式:
histogram
这可能适用于第一个直方图,然而,第二个直方图将绘制在同一个图上,它将取代第一个直方图;其他人一样。
可能的解决方案可能是:
histogram
figure
histogram
在第一种情况下,在每次调用figure
之前拨打histogram
就足够了。
在第二种情况下,您可以使用函数subplot在一个图中创建5个轴,用于绘制直方图。
在下文中,您可以找到所提议方法的可能实现。
两个标志用于控制绘图:
脚本的绘图部分可以更新如下:
% Define and set the flags to control the drawing mode:
% same_xy_lim: 1 => set the same xlim, ylim for all the axes
% 0 => do not modify the xlim, ylim
% multi_fig: 1 => plot each histogram in a separate figure
% 0 => plot all the histograms in a single figure using
% subplot
same_xy_lim=1;
multi_fig=1;
% figure();
if(multi_fig)
figure
else
subplot(3,2,1)
end
h1=histogram(ar1);
if(same_xy_lim)
xlim([0 1])
ylim([0 100])
end
if(multi_fig)
figure
else
subplot(3,2,2)
end
h2=histogram(ar2);
if(same_xy_lim)
xlim([0 1])
ylim([0 100])
end
if(multi_fig)
figure
else
subplot(3,2,3)
end
h3=histogram(ar3);
if(same_xy_lim)
xlim([0 1])
ylim([0 100])
end
if(multi_fig)
figure
else
subplot(3,2,4)
end
h4=histogram(ar4);
if(same_xy_lim)
xlim([0 1])
ylim([0 100])
end
if(multi_fig)
figure
else
subplot(3,2,5)
end
h5=histogram(ar5);
if(same_xy_lim)
xlim([0 1])
ylim([0 100])
end
根据上述标志的设置生成:
一体图
每个数字一个直方图
希望这有帮助,
Qapla&#39;
答案 1 :(得分:0)
以下是代码的更正确,简单,易读且可用的版本:
% initialising the five arrays to hold the averages of five probabilities
% of interests
ar = zeros(100,5);
for k = 1:100
% generating the required random numbers for the proble
% pi is the probablity in winning the ith game
p = cumprod(rand(4,1));
% initialising the variables in order to get the sum of all probabilties of interests and then we can get our respective averages
t = zeros(1,5);
% starting the loop for 50 tournaments
for count_tour = 1:50,
% Total probabilties of winning the ith game
W(1) = p(1);
W(2) = p(1)*(1+p(2)-p(1));
W(3) = p(1)*p(2)*p(3)+((p(1)*p(1))*(2-p(1)-p(2)))+((p(4))*(1-p(1))*(1-p(1)));
% probabilty that player had won the first game given that he won the second game
W(4) = (p(1)*p(2))/W(2);
% probabilty of winning all three games
W(5) = p(1)*p(2)*p(3);
% getting the sum of all probabilies in 50 tournaments
t = t+W;
end
% getting the averages of probabilties of interest in 50 tournaments
av = t./50;
ar(k,:)=ar(k,:)+av;
end
figure();
hold on
for k = 1:size(ar,2)
h(k) = histogram(ar(k,:));
end
hold off
结果如(例如):
实际上,根本不需要你的内部循环,它什么都不做,并且可以使用逐元素算法消除外部循环,因此可以将此代码缩短为更高效和紧凑的版本:
% generating the required random numbers for the proble
% pi is the probablity in winning the ith game
p = cumprod(rand(100,4));
% Total probabilties of winning the ith game
W(:,1) = p(:,1);
W(:,2) = p(:,1).*(1+p(:,2)-p(:,1));
W(:,3) = p(:,1).*p(:,2).*p(:,3)+((p(:,1).*p(:,1)).*(2-p(:,1)-p(:,2)))+...
((p(:,4)).*(1-p(:,1)).*(1-p(:,1)));
% probabilty that player had won the first game given that he won the second game
W(:,4) = (p(:,1).*p(:,2))./W(:,2);
% probabilty of winning all three games
W(:,5) = p(:,1).*p(:,2).*p(:,3);
figure();
hold on
for k = 1:size(W,2)
h(k) = histogram(W(k,:));
end
hold off
不改变代码中的任何计算,只需消除不必要的循环和变量。