如何使用MATLAB查找给定坐标的最近点? [英] How to find the nearest points to given coordinates with MATLAB?

查看:1858
本文介绍了如何使用MATLAB查找给定坐标的最近点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Matlab解决最小化问题,我想知道哪一个是最简单的解决方案.我一直在考虑的所有潜在解决方案都需要大量的编程工作.

I need to solve a minimization problem with Matlab and I'm wondering which is the easiest solution. All the potential solutions that I've been thinking in require lot of programming effort.

假设我有一个纬度/经度坐标点(A,B),我需要的是在经度/纬度坐标图中搜索与此点最近的点.

Suppose that I have a lat/long coordinate point (A,B), what I need is to search for the nearest point to this one in a map of lat/lon coordinates.

尤其是,纬度和经度数组是两个2030x1354元素(距离1公里)的矩阵,其想法是在那些矩阵中找到与坐标(A,B)的距离最小的唯一索引,即最接近给定坐标(A,B)的值.

In particular, the latitude and longitude arrays are two matrices of 2030x1354 elements (1km distance) and the idea is to find the unique indexes in those matrices that minimize the distance to the coordinates (A,B), i.e., to find the closest values to the given coordinates (A,B).

任何帮助将不胜感激.

谢谢!

推荐答案

这总是很有趣的:)

首先,只要

  • 您不需要知道实际距离
  • 您可以绝对确定您永远不会靠近极地地区
  • 并且永远不会接近±180°的子午线

对于给定的纬度,-180°和+ 180°经度实际上是同一点,因此仅查看角度之间的差异是不够的.在极地地区,这将是一个更大的问题,因为较大的经度差对实际距离的影响较小.

For a given latitude, -180° and +180° longitude are actually the same point, so simply looking at differences between angles is not sufficient. This will be more of a problem in the polar regions, since large longitude differences there will have less of an impact on the actual distance.

球面坐标对于导航,映射和类似操作非常有用且实用.但是,对于空间计算,例如您要计算的表面距离,球面坐标实际上非常麻烦.

Spherical coordinates are very useful and practical for purposes of navigation, mapping, and that sort of thing. For spatial computations however, like the on-surface distances you are trying to compute, spherical coordinates are actually pretty cumbersome to work with.

尽管直接使用角度进行这种计算是可能,但我个人认为这不是实用的:您通常必须具有很强的球形背景三角学,以及丰富的经验来了解其许多陷阱-很多情况下,您需要解决不稳定性或特殊点"(例如,极点),由于引入了三角函数而需要考虑的象限模糊性,等等

Although it is possible to do such calculations using the angles directly, I personally don't consider it very practical: you often have to have a strong background in spherical trigonometry, and considerable experience to know its many pitfalls -- very often there are instabilities or "special points" you need to work around (the poles, for example), quadrant ambiguities you need to consider because of trig functions you've introduced, etc.

我在大学里已经学会了做所有这一切,但是我也了解到,球形trig方法通常会引入复杂性,而在数学上并不是严格要求的,换句话说,球形trig是 不是 ,是对基本问题的最简单表示.

I've learned to do all this in university, but I also learned that the spherical trig approach often introduces complexity that mathematically speaking is not strictly required, in other words, the spherical trig is not the simplest representation of the underlying problem.

例如,如果将纬度和经度转换为3D笛卡尔X,Y,Z坐标,然后通过简单公式找到距离,则您的距离问题就变得微不足道了

For example, your distance problem is pretty trivial if you convert your latitudes and longitudes to 3D Cartesian X,Y,Z coordinates, and then find the distances through the simple formula

距离( a b )= R·arccos( a /| a b /| b |)

distance (a, b) = R · arccos( a/|a| · b/|b| )

其中 a b 是球体上的两个这样的笛卡尔向量.请注意| a | = | b | = R,其中R = 6371为地球半径.

where a and b are two such Cartesian vectors on the sphere. Note that |a| = |b| = R, with R = 6371 the radius of Earth.

在MATLAB代码中:

In MATLAB code:

% Some example coordinates (degrees are assumed)
lon = 360*rand(2030, 1354);
lat = 180*rand(2030, 1354) - 90;

% Your point of interest
P = [4, 54];

% Radius of Earth
RE = 6371;

% Convert the array of lat/lon coordinates to Cartesian vectors
% NOTE: sph2cart expects radians
% NOTE: use radius 1, so we don't have to normalize the vectors
[X,Y,Z] = sph2cart( lon*pi/180,  lat*pi/180, 1);

% Same for your point of interest    
[xP,yP,zP] = sph2cart(P(1)*pi/180, P(2)*pi/180, 1);

% The minimum distance, and the linear index where that distance was found
% NOTE: force the dot product into the interval [-1 +1]. This prevents 
% slight overshoots due to numerical artifacts
dotProd = xP*X(:) + yP*Y(:) + zP*Z(:);
[minDist, index] = min( RE*acos( min(max(-1,dotProd),1) ) );

% Convert that linear index to 2D subscripts
[ii,jj] = ind2sub(size(lon), index)

如果您坚持不转换到笛卡尔,而是直接使用经/纬,则必须使用Haversine公式,如

If you insist on skipping the conversion to Cartesian and use lat/lon directly, you'll have to use the Haversine formula, as outlined on this website for example, which is also the method used by distance() from the mapping toolbox.

现在,所有这些对整个地球都是有效的,提供,您会发现光滑的球形地球足够精确了.如果要包括地球的扁率或某些高阶形状模型(或上帝禁止,距离包括 terrain 在内),则需要做得多得多的事情.但我认为这不是您的目标:)

Now, all of this is valid for the whole Earth, provided you find the smooth spherical Earth accurate enough an approximation. If you want to include the Earth's oblateness or some higher order shape model (or God forbid, distances including terrain), you need to do far more complicated stuff. But I don't think that is your goal here :)

PS-如果您将我所做的所有事情都写出来,我不会感到惊讶,您可能会重新发现Haversine公式.我只希望能够仅根据第一原理就可以计算出像球体上的距离那么简单的东西,而不是根据您很久以前植入头部的一些黑匣子公式进行计算:)

PS - I wouldn't be surprised that if you would write everything out that I did, you'll probably re-discover the Haversine formula. I just prefer to be able to calculate something as simple as distances along the sphere from first principles alone, rather than from some black box formula you had implanted in your head sometime long ago :)

这篇关于如何使用MATLAB查找给定坐标的最近点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆