Finding both row and column indexes of nearest value

32 Ansichten (letzte 30 Tage)
Luís Henrique Bordin
Luís Henrique Bordin am 2 Apr. 2024 um 18:21
Kommentiert: Image Analyst am 3 Apr. 2024 um 23:30
Hi experts,
Please, could someone help me to find the 2 indexes, that is, the row and column indexes, corresponding to the nearest longitude and/or latitude? I could figure it out easily in a vector of 1D with find and dsearchn functions, but the problem is I cannot simply transform my matrix into a vector because longitude and latitudes are not constant all along the same row and column.
I have two matrices, one for the longitude, and another for the latitude. Both matrices are 224x164.
I need the row and column indexes of the nearest longitude of 25.983 on the longitude matrix; And idem for the latitude -84.64.
Thank you very much in advance!
Luis

Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Apr. 2024 um 18:33
To find the nearest row and column, subtract the reference value from your matrix and then use find. To be super explicit, here are the steps:
m = magic(7)
m = 7x7
30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
refValue = 27;
diffMatrix = abs(m - refValue)
diffMatrix = 7x7
3 12 21 26 17 8 1 11 20 20 18 9 0 2 19 21 19 10 1 8 10 22 13 11 2 7 9 18 14 12 3 6 15 17 23 6 4 5 14 16 24 15 5 4 13 22 25 16 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
minDifferenceValue = min(diffMatrix, [], 'all')
minDifferenceValue = 0
[row, column] = find(diffMatrix == minDifferenceValue)
row = 2
column = 6
  4 Kommentare
Luís Henrique Bordin
Luís Henrique Bordin am 3 Apr. 2024 um 14:38
I tried your suggestion but I couldn't handle. I just needed the intersection indexes of those lines, so I took the indexes == 1, and then I got the intersection indexes with polyxpoly function, and it worked.
[xlon, ylon] = find(ind_lon == 1);
[xlat, ylat] = find(ind_lat == 1);
[xi,yi] = polyxpoly(xlon, ylon, xlat, ylat)
Thank you anyway, it is always good to learn new alternatives.
Image Analyst
Image Analyst am 3 Apr. 2024 um 23:30
OK. Next time, include your data so we can get you answer(s) right away with your own actual data.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Geographic Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by