简体   繁体   中英

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. Each pair of x and y corresponds to a z value.

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?

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.

在此输入图像描述

Edit -1 : Explanation on how to map a linear z over a 2D XY grid

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. 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 .

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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