[英]Plotting a Matrix in 3D space in MATLAB without using mesh and surf type of plots
I am not familiar with MATLAB environment and I want to draw a matrix in a way that presents each cell of a matrix as a point in the 3D space. 我对MATLAB环境不熟悉,我想以一种将矩阵的每个单元表示为3D空间中的点的方式绘制矩阵。
For example present matrix "A " with the following points in 3D space: x=1, y=1 Z= 10 , x=1, y=3 Z=27 until reach the point x=3, y=3, z=26. 例如,存在3D空间中具有以下点的矩阵“ A”:x = 1,y = 1 Z = 10,x = 1,y = 3 Z = 27直到到达点x = 3,y = 3,z = 26。
A =
10 15 27
56 87 2
90 87 26
I do not want to use mesh and surf. 我不想使用网格和冲浪。 I am looking for diagram like plot3 digram.I tried plot3 but it does not show the value of z correctly. 我正在寻找类似plot3 digram的图。我尝试了plot3,但它没有正确显示z的值。
i=1:3;
j=1:3;
plot3(i,j,A(i,j))
In the above figure; 在上图中; 3 values are presented for when x=3 and y=3 however it should present these values for x=1,y=3; 当x = 3和y = 3时给出3个值,但是对于x = 1,y = 3应该给出这些值; x=2,y=3;x=3,y=3 X = 2,Y = 3; X = 3,Y = 3
To begin I would recommend you to change your variable names. 首先,我建议您更改变量名。 I'll use x
and y
instead of i
and j
. 我将使用x
和y
代替i
和j
。 In many language these symbols are more typically used for scalar index rather than full vector indices, and in Matlab they can have a special significance ( they are used to represent complex numbers ). 在许多语言中,这些符号更通常用于标量索引而不是全矢量索引,在Matlab中,它们可能具有特殊的意义( 它们用于表示复数 )。
That said, in your statement i=1:3;
也就是说,在您的陈述中, i=1:3;
you only generate 3 indices, but you have 9 values to plot in your matrix. 您只生成3个索引,但是要在矩阵中绘制9个值。 These 3 indices have to be repeated (3 times in your case, one for each column). 这3个索引必须重复(在您的情况下为3次,每列一次)。 So a proper x
and y
generation would be: 因此,正确的x
和y
代将是:
%% // Manual mesh/coordinate generation
x = bsxfun(@times,1:size(A,1), ones([size(A,2) 1])) ;
x = x(:) ;
y = bsxfun(@times, ones([1 size(A,1)]) , (1:size(A,2)).' ) ;
y = y(:) ;
With that you can use at your convenience scatter3
or plot3
: 这样您就可以方便地使用scatter3
或plot3
:
hscat = scatter3( x, y, A(:) ) ;
hp3 = plot3( x, y, A(:),'Marker','o','LineStyle','none') ;
%// will both produce exactly the same result
Now please consider the fact that the way I generated the x
and y
coordinate is nothing more than what meshgrid
would do for you (or the more dimension generic ndgrid
). 现在,请考虑以下事实:我生成x
和y
坐标的方式只不过是meshgrid
为您(或更具维度的泛型ndgrid
) ndgrid
。
For example, in the code below, the 3 plotting methods will produce exactly the same ouput than above, so just take your pick: 例如,在下面的代码中,这三种绘制方法将产生与上面完全相同的输出,因此请选择:
%% define a grid
[X,Y] = meshgrid( 1:size(A,1) , 1:size(A,2) ) ;
%% // surface plot (but only points visible, no line)
hsurf = surf(X,Y,A,'Marker','o','LineStyle','none','FaceColor','none') ;
%% // scatter3
hscat = scatter3( X(:), Y(:), A(:) ) ;
%% // plot3
hp3 = plot3( X(:), Y(:), A(:),'Marker','o','LineStyle','none') ;
Why reinvent the wheel when you have one at hand ... meshgrid
do the job for you in less code instruction ;-) 当您手头有个轮子时,为什么meshgrid
重新发明轮子... meshgrid
用更少的代码指令为您完成了工作;-)
To generate the coordinates, you should use ndgrid (or meshgrid , which swaps X
and Y
.): 要生成坐标,应该使用ndgrid (或meshgrid ,它交换X
和Y
):
[X, Y] = ndgrid(1:3, 1:3);
plot3
is going to connect the input points with a line, so if you want distinct points in your plot, use scatter3 : plot3
将用一条线连接输入点,因此,如果要在绘图中有不同的点,请使用scatter3 :
scatter3(X(:), Y(:), A(:));
(You can also use plot3
in the same way if you want the lines.) (如果需要线条,也可以以相同的方式使用plot3
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.