Matlab中两个向量的直方图 [英] Histogram from two vectors in Matlab
问题描述
预先感谢您的帮助.
我有两组平行向量:
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的长度可以不相等,并且不必包含相同的类别.
where x, y are the categories, and x_count, y_count are the frequency of each categories. x and y can be of unequal lengths, and need not contain the same categories.
我想创建一个并排的条形图/直方图,其中x轴是类别,并排放置,如下所示:
I want to create a side-by-side bar/histogram plot, where the x-axis is the categories, placed side-by-side like this: side by side multiply histogram in matlab. The frequency counts go along the y-axis.
我曾尝试使用Google搜索功能,但仍然坚持使用此功能.如果有人可以帮助,那就太好了. 在matlab中并排乘法直方图的解决方案仅在x和y的长度相同,但我的长度不相同.
I've tried googling around, but still stuck on this. If someone could help, that would be great. The solution in side by side multiply histogram in matlab works only if x and y have the same length, but mine's not.
推荐答案
您可以尝试以下方法:
% 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']);
这篇关于Matlab中两个向量的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!