【问题标题】:Not able to generate histogram in matlab using array无法使用数组在matlab中生成直方图
【发布时间】:2017-02-05 05:04:31
【问题描述】:

我无法在 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);

【问题讨论】:

  • 当我按原样运行您的代码时,我得到ar5 的直方图,这是我所期望的。所以你可能需要更清楚地解释你的目标。

标签: arrays matlab histogram


【解决方案1】:

假设计算数组ar1, ar2, ar3, ar4, ar5 的部分是正确的,并且还考虑到@EBH 的答案中提出的更新,问题可能出在您绘制直方图的方式上:

  • 你先打开一个图
  • 您按顺序调用函数 5 次 histogram

这可能适用于第一个直方图,但是,第二个将绘制在同一个图上,它将取代第一个;其他的也一样。

可能的解决方案是:

  • 将每个histogram 放在专用的figure
  • 一张图上的所有histogram

在第一种情况下,在每次调用 histogram 之前调用 figure 就足够了。

在第二种情况下,您可以使用函数subplot 在一个图形中创建 5 个轴来绘制直方图。

在下文中,您可以找到建议方法的可能实现。

两个标志用于控制绘图:

  • same_xy_lim: 1 => 为所有轴设置相同的 xlim、ylim 0 => 不修改 xlim, ylim
  • multi_fig: 1 => 在单独的图中绘制每个直方图 0 => 使用在一个图中绘制所有直方图 子情节

脚本的绘图部分可以更新如下:

% 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

这个生成,取决于上面提到的标志的设置:

合一图

每个图一个直方图

希望这会有所帮助,

卡普拉'

【讨论】:

    【解决方案2】:

    这是您的代码的更正确、简单、可读和有效的版本:

    % 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
    

    无需更改代码中的任何计算,只需消除不必要的循环和变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      相关资源
      最近更新 更多