[英]Generating all points of grid from given vectors of vertical and horizontal coordinates - MATLAB
I have a square grid of 10 by 10 points. 我有一个10 x 10点的正方形网格。 v1
is the vector of horizontal coordinates and v2
is the vector that contains the vertical coordinates. v1
是水平坐标的向量,而v2
是包含垂直坐标的向量。 From these two vectors, I want to construct all the 100 points. 从这两个向量,我想构造所有100个点。 Here is a 2 by 2 example: 这是一个2 x 2的示例:
v1 = [1 2];
v2 = [3 4];
Then the 4 points I want to generate are: 然后,我要生成的4个点是:
p(1,:) = [1,3]
p(2,:) = [1,4]
p(3,:) = [2,3]
p(4,:) = [2,4]
How can this be done in MATLAB? 如何在MATLAB中完成?
You will want to use meshgrid
or even better: ndgrid
. 您将要使用meshgrid
或更好的方法: ndgrid
。 The concept of both functions is the same, but ndgrid
is more general and has a sorting of the output that is often more useful. 这两个函数的概念是相同的,但是ndgrid
更通用,并且对输出进行排序通常更有用。
x = [1,2];
y = [3,4];
[X, Y] = ndgrid(x, y);
P = [X(:), Y(:)];
The call to ndgrid
will generate two matrices X
and Y
which will have the structure of the mesh you want, and the values X(i,j)
and Y(i,j)
correspond to each other. 调用ndgrid
将生成两个矩阵X
和Y
,它们将具有所需的网格结构,并且值X(i,j)
和Y(i,j)
对应。 So to get the points P
, you just need to reshape those arrays to column vectors and concatenate them. 因此,要获得点P
,只需要将这些数组重塑为列向量并将它们连接起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.