フィルターのクリア

How to find a value in a matrix

2 ビュー (過去 30 日間)
Honey
Honey 2021 年 11 月 27 日
コメント済み: Honey 2021 年 11 月 28 日
Hi
I have a matrix of latitude and another matrix of longtitude for the location of an area pixel to pixel. I have a location value (longtitue and latitude) that I want to find the position of the pixels which this value will be located in. How can I find this?
Look at below to the exemplary matrix of Latitude.
There is another matrix like this for longtitudeThe point that I am looking for is Latitude=35.6886 and Longtitude= 53.6113
  2 件のコメント
David Hill
David Hill 2021 年 11 月 27 日
Can't you just use the find() command?
Honey
Honey 2021 年 11 月 27 日
I tried this but it doesnt work.

サインインしてコメントする。

採用された回答

DGM
DGM 2021 年 11 月 27 日
Consider the simple example:
% two orthogonal grids
[x y] = meshgrid(linspace(0,1,10))
x = 10×10
0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1.0000
y = 10×10
0 0 0 0 0 0 0 0 0 0 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.2222 0.2222 0.2222 0.2222 0.2222 0.2222 0.2222 0.2222 0.2222 0.2222 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.4444 0.4444 0.4444 0.4444 0.4444 0.4444 0.4444 0.4444 0.4444 0.4444 0.5556 0.5556 0.5556 0.5556 0.5556 0.5556 0.5556 0.5556 0.5556 0.5556 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.7778 0.7778 0.7778 0.7778 0.7778 0.7778 0.7778 0.7778 0.7778 0.7778 0.8889 0.8889 0.8889 0.8889 0.8889 0.8889 0.8889 0.8889 0.8889 0.8889 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
% the point you're trying to locate
targetpt = [0.3333 0.5555]; % [x y]
tol = 0.001; % tolerance
% the row and column where the point is found
[tprow tpcol] = find(abs(x-targetpt(1))<tol & abs(y-targetpt(2))<tol)
tprow = 6
tpcol = 4
Don't expect simple equality tests to work with floating point numbers like this. You'll have to test matches to within some defined tolerance.
  5 件のコメント
DGM
DGM 2021 年 11 月 27 日
Ah yeah. Those aren't really close to the mesh at all. It depends on what you want to do. If you want to keep working on the fixed mesh and just find the nearest vertices, you can do something like:
X = xlsread('X,Y.xlsx','X');
Y = xlsread('X,Y.xlsx','Y');
yx = xlsread('querypoints.xlsx');
% X and Y are meshgrids; don't really need all that
Xv = X(:,1).';
Yv = Y(1,:);
% find the subscripts of the nearest match
[~,idxx] = min(abs(Xv-yx(:,2)),[],2); % index along dim 1
[~,idxy] = min(abs(Yv-yx(:,1)),[],2); % index along dim 2
% bear in mind the vector orientation
% here, "x" and "y" refer to the names from the spreadsheet
% not the array dimensions
% for example, look up the closest match for a selected query point
k = 1; % pick a point
yx(k,:)
ans = 1×2
53.6114 36.6867
X(idxx(k),idxy(k))
ans = 36.6550
Y(idxx(k),idxy(k))
ans = 53.6510
I had to rename the second file only because the web-version refused to recognize the filename. Otherwise it worked fine on desktop.
Honey
Honey 2021 年 11 月 28 日
Great ! It works and now with 'find()', I can have the pixel numbers too

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by