[英]Histogram of multiple dataset with different dimension in Matlab
您可以使用histogram()
function 并检索每个直方图的.binCounts
并以提供 10 x 3 数组的方式连接它们。 通过在这个 10 x 3 数组上调用bar()
,您将得到一个类似的分箱图,该图显示了 3 个数据集的直方图,分箱显示为三条。 使用histogram()
function 也是一个好主意,因为 MATLAB 不再推荐使用hist()
。
x1 = rand(1100,1);
x2 = rand(1000,1);
x3 = rand(900,1);
h1 = histogram(x1);
Counts_1 = h1.BinCounts;
h2 = histogram(x2);
Counts_2 = h2.BinCounts;
h3 = histogram(x3);
Counts_3 = h3.BinCounts;
Bin_Edges = h3.BinEdges;
Bin_Width = h3.BinWidth;
Bin_Centres = Bin_Edges(1:end-1) + Bin_Width/2;
Counts = [Counts_1.' Counts_2.' Counts_3.'];
bar(Counts);
title("Triple Bar Histogram");
xlabel("Bin Centres"); ylabel("Count");
set(gca,'xticklabel',Bin_Centres);
使用 MATLAB R2019b 运行
那么问题确实是你不能添加非大小匹配的变量。
这是一个适合您的补丁工作:神奇的是使您的代码匹配的 Nan 变量
% its bad habits but:
x1 = rand(1100,1);
x2 = rand(1000,1);
x3 = rand(900,1);
% make em' all the same size
max_size = max([length(x1),length(x2),length(x3)]);
% add in the ending nan
x1 = [x1; nan(max_size-length(x1), 1)];
x2 = [x2; nan(max_size-length(x2), 1)];
x3 = [x3; nan(max_size-length(x3), 1)];
% plot em' bad
x = [x1 x2 x3];
figure;
hist(x)
它现在工作得很好:(但你知道它有点像弗兰肯斯坦的杰作:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.