How to determine what grid cell a given point is in?

13 ビュー (過去 30 日間)
Darcy
Darcy 2015 年 7 月 16 日
コメント済み: Samson Williams 2021 年 9 月 30 日
I apologize if this question has been asked before but I have looked and cannot find an answer.
Lets say I have a grid with x-vector [1:10] and y-vector [1:10]. And now I have the point (3.994, 7.554).
Is there an easy way to determine which grid cell this point lies in? I am currently solving the problem with for loops to find the nearest x- and y-line to the point. But I am sure there is an embedded function which addresses the problem.
Any help is appreciated. Cheers
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2015 年 7 月 16 日
What is the expected result for this case?

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

採用された回答

Star Strider
Star Strider 2015 年 7 月 16 日
One possibility:
x_vector = [1:10];
y_vector = [1:10];
point = [3.994, 7.554];
x_grid = find(x_vector <= point(1), 1, 'last');
y_grid = find(y_vector <= point(2), 1, 'last');
x_grid = [x_grid x_grid+1]
y_grid = [y_grid y_grid+1]
The output are the final values of ‘x_grid’ and ‘y_grid’
  4 件のコメント
Mateus Mengatto
Mateus Mengatto 2021 年 3 月 9 日
Perfect! Thanks
Star Strider
Star Strider 2021 年 3 月 9 日
Mateus Mengatto — My pleasure!

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 7 月 16 日
You can use
[xx,yy]=meshgrid(x,y)
  1 件のコメント
Samson Williams
Samson Williams 2021 年 9 月 30 日
Can you please elaborate on this answer? It is too vague to understand what you mean.

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


Walter Roberson
Walter Roberson 2015 年 7 月 16 日
For values no smaller than 1:
[floor(x), floor(y)]
More generally,
[~, binx] = histc(x, [x_values(:); inf]);
[~, biny] = histc(y, [y_values(:); inf]);
this assumes that values might be greater than x_values(end) -- for example with x_values = 1:10 then this code assumes that 10.32403 might be a valid input. If that is not the case you need to decide whether x_values(end) exactly is a valid input and if so whether you want it to go into the bin that starts at x_values(end-1) -- e.g., should 10 exactly go into the same bin as [9,10) or should it go into a bin by itself. If you want it to go into the same bin as [9,10), making that bin [9,10] (inclusive on both sides), an asymmetry as all the other bins will be semi-open, then
[~, binx] = histc(x, [reshape(x_values(1:end-1),[],1); inf]);
Also this assumes that there are no values lower than x_values(1). If there are then do you want them discarded or do you want them placed in a bin by themselves, or do you want them in the same bin as the first defined range?

カテゴリ

Help Center および File ExchangePolar Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by