简体   繁体   English

Matlab中不同维度的多个数据集的直方图

[英]Histogram of multiple dataset with different dimension in Matlab

I can plot multiple plots with a same dimension in a histogram我可以在直方图中 plot 多个具有相同维度的图

x = rand(1000,3);
hist(x);

在此处输入图像描述

But I could not plot multiple plots with a different dimension.但我不能 plot 多个不同尺寸的地块。

x1 = rand(1100,1);
x2 = rand(1000,1);
x3 = rand(900,1);
x = [x1 x2 x3]
hist(x)

I get the following error我收到以下错误

Error using horzcat
Dimensions of arrays being concatenated are not consistent.

Please someone point me to right direction to solve the problem.请有人指出我解决问题的正确方向。

Triple Bar Histogram (3 datasets)三重直方图(3 个数据集)

You can use the histogram() function and retrieve the .binCounts of each histogram and concatenate them in a fashion that gives a 10 by 3 array.您可以使用histogram() function 并检索每个直方图的.binCounts并以提供 10 x 3 数组的方式连接它们。 By calling bar() on this 10 by 3 array you'll get a similar binning graph that shows the histogram of 3 datasets with the bins shown as triple bars.通过在这个 10 x 3 数组上调用bar() ,您将得到一个类似的分箱图,该图显示了 3 个数据集的直方图,分箱显示为三条。 Also a good idea to use the histogram() function as the use of hist() is no longer recommended by MATLAB.使用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);

Ran using MATLAB R2019b使用 MATLAB R2019b 运行

Well the problem is indeed that you cant add non size matching vars.那么问题确实是你不能添加非大小匹配的变量。

Here is a patch work for you: the magic is the Nan variable that make your code match这是一个适合您的补丁工作:神奇的是使您的代码匹配的 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)

Its working great now: (but you know its a bit of a Frankenstein masterpiece :-)它现在工作得很好:(但你知道它有点像弗兰肯斯坦的杰作:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM