簡體   English   中英

Matlab中不同維度的多個數據集的直方圖

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

我可以在直方圖中 plot 多個具有相同維度的圖

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

在此處輸入圖像描述

但我不能 plot 多個不同尺寸的地塊。

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

我收到以下錯誤

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

請有人指出我解決問題的正確方向。

三重直方圖(3 個數據集)

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM