Finding both row and column indexes of nearest value

조회 수: 32 (최근 30일)
Luís Henrique Bordin
Luís Henrique Bordin 2024년 4월 2일 18:21
댓글: Image Analyst 2024년 4월 3일 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

채택된 답변

Image Analyst
Image Analyst 2024년 4월 2일 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
Luís Henrique Bordin
Luís Henrique Bordin 2024년 4월 3일 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 2024년 4월 3일 23:30
OK. Next time, include your data so we can get you answer(s) right away with your own actual data.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by