首页 文章

来自Matlab中两个向量的直方图

提问于
浏览
1

在此先感谢您的帮助 .

我有两组平行向量:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 55]; 
x_count = [7721, 6475, 3890, 2138, 1152, 784, 674, 492, 424, 365, 309, 302, 232, 250, 220, 208, 190, 162, 144, 134, 97, 93, 89, 97, 92, 85, 77, 87, 64, 75, 72, 82, 61, 48, 46, 44, 35, 20, 28, 20, 21, 10, 6, 8, 4, 4, 4, 3, 1, 1];

y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 55];    
y_count = [88, 40, 24, 12, 8, 5, 1, 1, 1, 100];

其中x,y是类别,x_count,y_count是每个类别的频率 . x和y可以是不相等的长度,并且不需要包含相同的类别 .

我想创建一个并排的条形图/直方图,其中x轴是类别,并排放置如下:side by side multiply histogram in matlab . 频率计数沿y轴 .

我试过谷歌搜索,但仍坚持这一点 . 如果有人可以提供帮助,那就太好了 . 只有当x和y具有相同的长度时,side by side multiply histogram in matlab中的解决方案才有效,但我的不是 .

1 回答

  • 3

    你可以试试这个:

    % create unique bins
    bins = unique([x y]); 
    
    % create vectors with zeros same size as bins
    xBins = zeros(size(bins));
    yBins = zeros(size(bins)); 
    
    % fill in counts in the respective spots
    xBins(ismember(x, bins)) = x_count;
    yBins(ismember(y, bins)) = y_count;
    
    
    bar(bins, [xBins' yBins']);
    

相关问题