简体   繁体   English

根据垂直和水平坐标的给定向量生成网格的所有点-MATLAB

[英]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将生成两个矩阵XY ,它们将具有所需的网格结构,并且值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 ,只需要将这些数组重塑为列向量并将它们连接起来。

If you have the Neural Network Toolbox you are looking for the combvec function, which creates all combinations from 2 vectors. 如果您有神经网络工具箱,那么您正在寻找combvec函数,该函数从2个向量创建所有组合。

Example: 例:

v1=[1 2] 

v2=[3 4]

V = combvec(v1,v2)

which outputs: 输出:

V =

     1     2     1     2
     3     3     4     4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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