简体   繁体   中英

MATLAB find row and column index of closest to specified value

I have latitude LAT , longitude LON and windspeed from a netCDF file.

I want to find the windspeed at a given LAT,LON coordinate %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. When I have the nearest values in LAT and LON I will then use the addresses to locate my windspeed at this location.

When I use the code below, I expect a single value for each of the column and row values CLON, CLAT, RLAT and RLON . 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.

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. Without any comparison operator, find assumes a logical comparison and will return a vector containing the indices of all non-zero elements. That is not what you want.

Try the following :

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

where eps is an error tolerance, for instance 0.00001.

You can't give find a perfect equality because the figure you are seeking may be not present in the matrix. With abs(LAT - RefLAT) , you will have a matrix of difference between two values. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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