简体   繁体   English

MATLAB查找最接近指定值的行和列索引

[英]MATLAB find row and column index of closest to specified value

I have latitude LAT , longitude LON and windspeed from a netCDF file. 我有一个netCDF文件中的纬度LAT ,经度LON和风速。

I want to find the windspeed at a given LAT,LON coordinate %Location of Met Mast 51.94341,1.922094888 . 我想找到给定的LAT,LON坐标处的风速%Location of Met Mast 51.94341,1.922094888 I am trying to find the nearest value to RefLAT and RefLON in the LAT and LON matrices respectively. 我试图分别在LATLON矩阵中找到最接近RefLATRefLON值。 When I have the nearest values in LAT and LON I will then use the addresses to locate my windspeed at this location. 当我在LAT和LON中具有最接近的值时,我将使用这些地址在该位置定位风速。

When I use the code below, I expect a single value for each of the column and row values CLON, CLAT, RLAT and RLON . 当我使用下面的代码时,我期望列和行值CLON, CLAT, RLATRLON每个值都有一个值。 Instead I get CLON, CLAT, RLAT and RLON as arrays of 972 values, the matrices I am searching LAT and LON are size 848 x 972. 相反,我将CLON, CLAT, RLATRLON为972个值的数组,我正在搜索LATLON的矩阵的大小为848 x 972。

LAT = ncread('wind_level2.nc','latitude');
LON = ncread('wind_level2.nc','longitude');
wind = ncread('wind_level2.nc','wind');

LAT = double(LAT);
LON =double(LON);
%Location of Met Mast 51.94341,1.922094888

RefLAT=51.94341;
LATcalc = abs(LAT - RefLAT);
[RLAT,CLAT]=find(min(LATcalc));

RefLON=1.922094888;
LONcalc = abs(LON - RefLON);
[RLON,CLON]=find(min(LONcalc));`

Any help appreciated. 任何帮助表示赞赏。 Thanks 谢谢

Data sample as requested: 根据要求提供数据样本:

LAT: 848x972 double:

51.6652641296387    51.6608505249023    51.6564369201660    51.6520233154297    51.6476097106934    51.6431961059570    51.6387825012207
    51.6663322448731    51.6619186401367    51.6575050354004    51.6530914306641    51.6486778259277    51.6442642211914    51.6398506164551
    51.6674041748047    51.6629867553711    51.6585731506348    51.6541595458984    51.6497459411621    51.6453323364258    51.6409187316895
    51.6684722900391    51.6640548706055    51.6596412658691    51.6552276611328    51.6508140563965    51.6464004516602    51.6419868469238
    51.6695404052734    51.6651229858398    51.6607093811035    51.6562957763672    51.6518821716309    51.6474685668945    51.6430549621582
    51.6706047058106    51.6661911010742    51.6617774963379    51.6573638916016    51.6529502868652    51.6485366821289    51.6441192626953
    51.6716728210449    51.6672592163086    51.6628456115723    51.6584320068359    51.6540145874023    51.6496009826660    51.6451873779297
    51.6727409362793    51.6683235168457    51.6639099121094    51.6594963073731    51.6550827026367    51.6506690979004    51.6462554931641
    51.6738052368164    51.6693916320801    51.6649780273438    51.6605644226074    51.6561470031738    51.6517333984375    51.6473197937012
    51.6748695373535    51.6704559326172    51.6660423278809    51.6616287231445    51.6572151184082    51.6528015136719    51.6483840942383
    51.6759376525879    51.6715240478516    51.6671066284180    51.6626930236816    51.6582794189453    51.6538658142090    51.6494522094727

LON 848x972 double:

3.04663085937500    3.04491543769836    3.04320049285889    3.04148554801941    3.03977084159851    3.03805613517761    3.03634166717529
3.03959774971008    3.03788304328918    3.03616857528687    3.03445434570313    3.03274011611938    3.03102612495422    3.02931237220764
3.03256440162659    3.03085041046143    3.02913665771484    3.02742290496826    3.02570939064026    3.02399611473084    3.02228283882141
3.02553081512451    3.02381753921509    3.02210426330566    3.02039122581482    3.01867818832397    3.01696562767029    3.01525306701660
3.01849699020386    3.01678419113159    3.01507163047791    3.01335930824280    3.01164698600769    3.00993490219116    3.00822281837463
3.01146292686462    3.00975084304810    3.00803875923157    3.00632715225220    3.00461530685425    3.00290393829346    3.00119256973267
3.00442862510681    3.00271701812744    3.00100588798523    2.99929451942444    2.99758362770081    2.99587273597717    2.99416208267212
2.99739408493042    2.99568319320679    2.99397253990173    2.99226188659668    2.99055147171021    2.98884129524231    2.98713135719299
2.99035930633545    2.98864889144897    2.98693895339966    2.98522901535034    2.98351931571960    2.98180961608887    2.98010015487671
2.98332428932190    2.98161458969116    2.97990512847900    2.97819590568543    2.97648668289185    2.97477769851685    2.97306895256043

As noted by @excaza, your syntax of find is not appropriate in your case. 如@excaza所述,您的find语法不适合您的情况。 Without any comparison operator, find assumes a logical comparison and will return a vector containing the indices of all non-zero elements. 如果没有任何比较运算符, find进行逻辑比较并返回一个包含所有非零元素索引的向量。 That is not what you want. 那不是你想要的。

Try the following : 尝试以下方法:

[RLAT,CLAT]=find(LATcalc<eps);

where eps is an error tolerance, for instance 0.00001. 其中eps是一个误差容限,例如0.00001。

You can't give find a perfect equality because the figure you are seeking may be not present in the matrix. find完美的相等性,因为您要查找的图形可能不存在于矩阵中。 With abs(LAT - RefLAT) , you will have a matrix of difference between two values. 使用abs(LAT - RefLAT) ,您将获得两个值之间的差异矩阵。 As before, you may not have a perfect zero so you find the closest result to zero by an error tolerance low enough to be sure to catch the minimum. 和以前一样,您可能没有理想的零,因此您可以通过足够低的容错能力找到最接近零的结果,以确保能够捕捉到最小值。

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

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