簡體   English   中英

在給定表格的情況下在 Matlab 中繪制 3d 圖形

[英]Plotting a 3d figure in Matlab given a table

我的桌子是這樣的:桌子的圖像

X↓ Y-> Y = 0 Y = 1 Y = 2 Y = 3
X = 3 P XY = 4/54 P XY = 3/54 P XY = 2/54 P XY = 1/54
X = 5 P XY = 6/54 P XY = 5/54 P XY = 4/54 P XY = 3/54
X = 7 P XY = 8/54 P XY = 7/54 P XY = 6/54 P XY = 5/54

P XY由 (x - y + 1)/54 給出

我應該如何在 MATLAB 上繪制這個圖形? 我想繪制一個 3d 直方圖,如下所示:

陰謀 .

我的大多數嘗試都以錯誤告終,通常是因為 X 和 Y 向量的長度不同。

如果有方法也可以做這個 Python,那么請告訴我,這也有效。

在 Matlab 中,您可以使用histogram2指定 x 和 y 方向上的 bin 邊緣以及 bin 計數。

% Your data
x = [3; 5; 7];
y = [0, 1, 2, 3];
p = (x - y + 1)/54;
% since x is a column vector and y a row vector, as of Matlab R2016b this results in a length(x)*length(y) matrix, equivalent to:
% p = zeros(length(x), length(y));
% for ix = 1:length(x)
%    for iy = 1:length(y)
%        p(ix, iy) = (x(ix) - y(iy) + 1)/54;
%    end
% end

% Plot the histogram
histogram2('XBinEdges', [x; 9], ...
           'YBinEdges', [y, 4], ...
           'BinCounts', p);
xlabel('X')
ylabel('Y')
zlabel('P_{xy}')

由於 x 需要 3 個 bin(y 需要 4 個 bin),因此您需要提供 4 個邊(y 為 5 個邊),因此histogram2調用中的附加點。

編輯:再想一想,您可能希望垃圾箱以您提供的 x 和 y 點為中心,因此您可能想要使用xEdges = [x - 1; x(end) + 1] xEdges = [x - 1; x(end) + 1]yEdges = [y - 0.5, y(end) + 0.5]作為 bin 邊緣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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