【问题标题】:Count strings occurrences and plot histogram计算字符串出现次数并绘制直方图
【发布时间】:2012-08-11 14:54:53
【问题描述】:

是否有任何直接的方法可以从像下面这样的元胞数组创建直方图?连续条之间的间距应完全相同,x轴的标签应为垂直方向下方变量的对应名称。

'w464'
'w462'
'w461'
'w464'
'w461'
'w463'
'w466'
'w461'

【问题讨论】:

    标签: matlab histogram cell


    【解决方案1】:

    你也可以使用直方图函数如下:

    [C,~,ic] = unique(A);
    
    fig1 = figure;
    axes1 = axes('Parent',fig1,'XTickLabel',C,'XTick',1:length(C));
    hold(axes1,'on');
    
    histogram(ic)
    

    【讨论】:

      【解决方案2】:

      只使用内置函数的解决方案

      [u,~,n] = unique(A(:));
      B = accumarray(n, 1, [], @sum);
      bar(B)
      set(gca,'XTickLabel',u)
      

      【讨论】:

        【解决方案3】:

        如果您可以访问统计工具箱,grp2idx 非常有用:

        %# sorting is only necessary if the output should be sorted as well
        [idx,label] = grp2idx(sort(A)) 
        
        hist(idx,unique(idx));
        set(gca,'xTickLabel',label)
        

        【讨论】:

          【解决方案4】:

          我也想知道一个更好的方法。 Fwiw,我以迂回的方式使用countmember 来绘制这样的数据。 IE。如果您发布的数据名为A

          >> B={sort(unique(A)) countmember(sort(unique(A)),A)};
          >> bar(B{2});
          >> set(gca,'XTickLabel',B{1})
          

          【讨论】:

          • 哦,要更改标签的方向,请从文件交换中查看 Rotate X-axis tick labels
          • 再次感谢您!你是我的英雄! :D
          • @AGS:使用grp2idx 有一种更简单的方法。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-24
          • 2014-04-24
          • 2019-08-23
          • 2012-12-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多