[英]MATLAB - discrete 3D stem plot?
I am trying to plot a discrete 3D stem plot where x
and y
are integers, and z
is a probability. 我试图绘制一个离散的 3D茎图,其中
x
和y
是整数, z
是概率。 Each pair of x
and y
corresponds to a z
value. 每对
x
和y
对应于z
值。
For the ease of demonstration, let's say they have the following correspondences. 为了便于演示,我们假设它们具有以下对应关系。
x = [1 2 3 4 5];
y = [1 2 3 4 5];
z = [0.1 0.1 0.1 0.1 ... 0.1]; % totally 25 terms
% 1st z value corresponds to the pair (1st_x_val, 1st_y_val)
How may I do it in MATLAB? 我怎么能在MATLAB中做到这一点?
Code 码
x = [1 2 3 4 5];
y = [1 2 3 4 5];
z = repmat(0.25,[1 25]);
z = reshape(z,[5 5]);
[x,y] = meshgrid(x,y);
stem3(x,y,z)
Basically with meshgrid
you are making linear combinations between all x's and all y's, thus making 25 combinations for which you have 25 z's. 基本上使用
meshgrid
你在所有x和所有y之间进行线性组合,从而产生25个组合,你有25个z。
Edit -1 : Explanation on how to map a linear z
over a 2D XY grid 编辑-1:有关如何在2D XY网格上映射线性
z
说明
Test Code 测试代码
x = 1:3;
y = 1:5;
z = 1:15;
z = reshape(z,[numel(y) numel(x)]);
[x,y] = meshgrid(x,y);
stem3(x,y,z)
xlabel('X -AXIS')
ylabel('Y -AXIS')
Output 产量
As one can see how indexing works here - For the first five values, X
stays the same as y
varies from 1 to 5 and so on for next 5 values. 正如我们可以看到索引在这里如何工作 - 对于前五个值,
X
保持不变, y
从1变为5,依此类推接下来的5个值。 Thus, if one wants to map a linear z
over a 2D XY grid, the reshaping would have the first element as number of element in y
and second would be corresponding number for x
. 因此,如果想要在2D XY网格上映射线性
z
,则重新整形将具有第一元素作为y
的元素数量,并且第二元素将是x
对应数字。
What's wrong with this? 这有什么问题?
x = [1 2 3 4 5];
y = [1 2 3 4 5];
[X,Y]=meshgrid(x,y);
Z=0.1*ones(size(X));
stem3(X,Y,Z)
I don't understand how the distinction between continuous/discrete is meaningful here though? 我不明白连续/离散之间的区别在这里有何意义?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.