在Matlab中绘制2D网格 [英] Drawing 2D grid in matlab
问题描述
我试图使用 x> = -1 和 y< == 1 的matlab获取2D网格,步长为0.1 但是我正在获得没有适当步长的3D网格.有什么想法吗?
I am trying to get a 2D grid using matlab with x >= -1 and y <= 1 with step size of 0.1 But I'm getting 3D grid with no proper step sizes. Any ideas?
我的代码:
[x, y] = meshgrid(-1:0.1:5, 0:0.1:1);
surf(x,y)
推荐答案
您是否只想绘制一堆2D点?您使用 plot
.以您的示例为例,您将获取x,y
点,并简单地为每个点放置点标记.在执行此操作之前,请先将它们转换为一维数组:
Do you just want to plot a bunch of 2D points? You use plot
. Using your example, you would take your x,y
points and simply put dot markers for each point. Convert them into 1D arrays first before you do this:
[X,Y] = meshgrid(-1:0.1:5, 0:0.1:1);
X = X(:);
Y = Y(:);
plot(X,Y,'b.');
xlabel('X'); % // Label the X and Y axes
ylabel('Y');
这就是我得到的:
如果要将此网格旋转一个角度,则可以使用旋转矩阵,并将其与每对(x,y)
坐标相乘.如果您想从线性代数中回想起,要逆时针旋转点,可以执行以下矩阵乘法:
If you want to rotate this grid by an angle, you would use a rotation matrix and multiply this with each pair of (x,y)
co-ordinates. If you recall from linear algebra, to rotate a point counter-clockwise, you would perform the following matrix multiplication:
[x'] = [cos(theta) -sin(theta)][x]
[y'] [sin(theta) cos(theta)][y]
x,y
是原始坐标,而x',y'
是旋转角度theta
之后的输出坐标.如果要旋转-30度(顺时针旋转30度),只需指定theta = -30 degrees
.请记住,cos
和sin
的角度为弧度,因此实际上是-pi/6
弧度.您需要做的是将每个点放入2D矩阵中.然后,您将使用旋转矩阵并将其应用于每个点.这样,您将向量化解决方案,而不是使用for
循环.因此,您可以这样做:
x,y
are the original co-ordinates while x',y'
are the output co-ordinates after rotation of an angle theta
. If you want to rotate -30 degrees (which is 30 degrees clockwise), you would just specify theta = -30 degrees
. Bear in mind that cos
and sin
take in their angles as radians, so this is actually -pi/6
in radians. What you need to do is place each of your points into a 2D matrix. You would then use the rotation matrix and apply it to each point. This way, you're vectorizing the solution instead of... say... using a for
loop. Therefore, you would do this:
theta = -pi/6; % // Define rotation angle
rot = [cos(theta) -sin(theta); sin(theta) cos(theta)]; %// Define rotation matrix
rotate_val = rot*[X Y].'; %// Rotate each of the points
X_rotate = rotate_val(1,:); %// Separate each rotated dimension
Y_rotate = rotate_val(2,:);
plot(X_rotate, Y_rotate, 'b.'); %// Show the plot
xlabel('X');
ylabel('Y');
这就是我得到的:
如果要执行其他转换,例如缩放每个轴,则只需将X
或Y
坐标乘以适当的比例即可:
If you wanted to perform other transformations, like scaling each axis, you would just multiply either the X
or Y
co-ordinates by an appropriate scale:
X_scaled = scale_x*X;
Y_scaled = scale_y*Y;
X_scaled
和Y_scaled
是坐标的缩放版本,而scale_x
和scale_y
是所需每个维度的缩放比例.如果要平移坐标,则可以将每个尺寸相加或相减一些数字:
X_scaled
and Y_scaled
are the scaled versions of your co-ordinates, with scale_x
and scale_y
are the scales in each dimension you want. If you wanted to translate the co-ordinates, you would add or subtract each of the dimensions by some number:
X_translate = X + X_shift; %// Or -
Y_translate = Y + Y_shift; %// Or -
X_translate
和Y_translate
是转换后的坐标,而X_shift
和Y_shift
是每个尺寸上想要的偏移量.请注意,您可以根据自己的需要执行+
或-
.
X_translate
and Y_translate
are the translated co-ordinates, while X_shift
and Y_shift
are the amount of shifts you want per dimension. Note that you either do +
or -
, depending on what you want.
这篇关于在Matlab中绘制2D网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!