简体   繁体   English

在matlab网格中找到最近的点

[英]Find closest point in matlab grid

G'day 天儿真好

I'm trying to program a smart way to find the closest grid points to the points along a contour. 我正在尝试编写一种智能方法来找到沿着轮廓的点的最近网格点。

The grid is a 2-dimensional grid, stored in x and y (which contain the x and y kilometre positions of the grid cells). 网格是一个二维网格,存储在xy (包含网格单元的x和y公里位置)中。

The contour is a line, made up of x and y locations, not necessarily regularly spaced. 轮廓是由x和y位置组成的线,不一定是规则间隔的。

This is shown below - the red dots are the grid, and the blue dots are the points on the contour. 如下所示 - 红点是网格,蓝点是轮廓上的点。 How do I find the indices of the red dot closest to each blue dot? 如何找到最接近每个蓝点的红点的索引?

插入最接近每个蓝点的红点

Edit - I should mention that the grid is a latitude/longitude grid, of an area fairly close to the south pole. 编辑 - 我应该提到网格是纬度/经度网格,是一个非常接近南极的区域。 So, the points (the red dots) are the position in metres from the south pole (using a polar stereographic representation). 因此,点(红点)是距离南极的米的位置(使用极坐标立体图)。 Since the grid is a geographic grid there is unequal grid spacing - with slightly different shaped cells (where the red dots define the vertices of the cells) due to the distortion at high latitudes. 由于网格是地理网格,因此网格间距不相等 - 由于高纬度地区的扭曲,形状单元格略有不同(红点定义了单元格的顶点)。 The result is that I can't just find which row/column of the x and y matrix corresponds closest to the input point coordinates - unlike a regular grid from meshgrid , the values in the rows and columns vary... 结果是我不能只找到xy矩阵的哪一行/哪一列最接近输入点坐标 - 与meshgrid的常规网格meshgrid ,行列中的值变化...

Cheers Dave 干杯戴夫

The usual method is to go: 通常的方法是:

for every blue point {
    for every red point {
        is this the closest so far
    }
}

But a better way is to put the red data into a kd tree. 但更好的方法是将红色数据放入kd树中。 This is a tree that splits the data along its mean, then splits the two data sets along their means etc until you have them separated into a tree structure. 这是一个沿着它的平均值分割数据的树,然后沿着它们的平均值分割两个数据集,直到你将它们分成树结构。

在此输入图像描述

This will change your searching effeciancy from O(n*m) to O(log(n)*m) 这会将您的搜索效率从O(n*m)改为O(log(n)*m)

Here is a library: 这是一个图书馆:

http://www.mathworks.com.au/matlabcentral/fileexchange/4586-kd-tree http://www.mathworks.com.au/matlabcentral/fileexchange/4586-kd-tree

This library will provide you the means to easily make a kd tree out of the data and to search for the closest point in it. 该库将为您提供从数据中轻松制作kd树并搜索其中最近点的方法。

Alternatively you can use a quadtree, not as simple but the same idea. 或者你可以使用四叉树,不是简单但相同的想法。 (you may have to write your own library for that) (你可能要为此编写自己的库)

Make sure the largest data set (in this case your red points) go into the tree as this will provide the greatest time reduction. 确保最大的数据集(在这种情况下是您的红点)进入树中,因为这将减少最大的时间。

I think I've found a way to do it using the nearest flag of griddata . 我想我找到了一种方法,使用nearestgriddata标志来做到这griddata

I make a matrix that is the same size as the grid x and y matrices, but is filled with the linear indices of the corresponding matrix element. 我创建了一个与网格xy矩阵大小相同的矩阵,但是填充了相应矩阵元素的线性索引。 This is formed by reshaping a vector (which is 1:size(x,1)*size(x,2) ) to the same dimensions as x . 这是通过将矢量( 1:size(x,1)*size(x,2) )整形为与x相同的尺寸而形成的。

I then use griddata and the nearest flag to find the linear index of the point closest to each point on my contour (blue dots). 然后我使用griddatanearest标志来找到最接近我的轮廓上每个点的点的线性索引(蓝点)。 Then, simply converting back to subscript notation with ind2sub leaves me with a 2 row vectors describing the matrix subscripts for the points closest to each point on the blue-dotted contour. 然后,简单地用ind2sub转换回下标符号留给我一个2行向量,描述最接近蓝点轮廓上每个点的点的矩阵下标。

This plot below shows the contour (blue dots), the grid (red dots) and the closest grid points (green dots). 下图显示了轮廓(蓝点),网格(红点)和最近的网格点(绿点)。

网格化的结果

This is the code snippet I used: 这是我使用的代码片段:

index_matrix1 = 1:size(x,1)*size(x,2); 
index_matrix1 = reshape(index_matrix1,size(x));
lin_ind = griddata(x,y,index_matrix1,CX,CY,'nearest'); % where CX and CY are the coords of the contour
[sub_ind(1,:),sub_ind(2,:)] = ind2sub(size(x),lin_ind);

I suppose that in the stereographic representation, your points form a neat grid in r - theta coordinates. 我想在立体表示中,你的点在r - theta坐标中形成一个整齐的网格。 (I'm not too familiar with this, so correct me if I'm wrong. My suggestion may still apply). (我对此不太熟悉,如果我错了,请纠正我。我的建议可能仍然适用)。

For plotting you convert from the stereographic to latitude-longitude, which distorts the grid. 对于绘图,您可以从立体图转换为纬度 - 经度,从而扭曲网格。 However, for finding the nearest point, consider converting the latitude-longitude of the blue contour points into stereographic coordinates, where it is easy to determine the cell for each point using its r and theta values. 但是,为了找到最近的点,可以考虑将蓝色轮廓点的纬度 - 经度转换为立体坐标,其中很容易使用其rtheta值确定每个点的单元格。

If you can index the cell in the stereographic representation, the index will be the same when you transform to another representation. 如果可以在立体表示中索引单元格,则转换为另一个表示时索引将相同。

The main requirement is that under some transformation, the grid points are defined by two vectors, X and Y , so that for any x in X and y in Y , (x, y) is a grid point. 主要的要求是,在一些变换,网格点由两个向量,定义XY ,使得对于任何xXyY(x, y)是一个网格点。 Next transform both the grid and the contour points by that transformation. 接下来通过该变换转换网格和轮廓点。 Then given an arbitrary point (x1, y1) , we can find the appropriate grid cell by finding the closest x to x1 and the closest y to y1 . 然后给定一个任意点(x1, y1) ,我们可以通过找到最接近xx1和最接近yy1来找到合适的网格单元。 Transform back to get the points in the desired coordinate system. 变换回以获得所需坐标系中的点。

dsearchn : ND nearest point search. dsearchn :ND最近点搜索。

[k, d] = dsearchn(A,B) : returns the distances, d, to the closest points. [k, d] = dsearchn(A,B) :将距离d返回到最近的点。 d is a column vector of length p. d是长度为p的列向量。

http://au.mathworks.com/help/matlab/ref/dsearchn.html?s_tid=gn_loc_drop http://au.mathworks.com/help/matlab/ref/dsearchn.html?s_tid=gn_loc_drop

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

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