简体   繁体   English

MATLAB在XY平面中移动点

[英]MATLAB moving a point in the XY plane

In MATLAB, I have an XY plane that is represented by finite number of points. 在MATLAB中,我有一个由有限数量的点表示的XY平面。 The possible values of x are stored in a vector X and the possible values of y are stored in another vector Y. I have a point, say A, such that A(1) belongs to X is the x-coordinate and A(2) belongs to Y is the y-coordinate. x的可能值存储在向量X中,y的可能值存储在另一个向量Y中。我有一个点,说A,这样A(1)属于X的x坐标,而A(2 )属于Y是y坐标。

This point A can move in one of 8 ways if it is in the middle: 如果A点位于中间,则它可以8种方式之一移动:

          .   .   .                      .   A                .    .

          .   A   .        OR            .   .       OR       .    A

          .   .   .                                           .     .

Of course, the set of these points changes if the point A is on the edge (sometimes only 5, sometimes only 3 if it is a corner). 当然,如果点A在边上(有时只有5个,如果是角则只有3个),这些点的集合也会改变。 How can I find the set of these "1-hop" neighboring points? 如何找到这些“ 1跳”相邻点的集合? What about the set of "k-hop" neighboring points? 那套“ k-hop”相邻点呢? By set I mean two vectors one for x-coordinates and another for y-coordinates. 所谓集合,是指两个向量,一个用于x坐标,另一个用于y坐标。 Thanks! 谢谢!

Consider the following code: 考虑以下代码:

%# create grid of 2D coordinates
sz = [5 6];
[X,Y] = meshgrid(1:sz(2),1:sz(1));

%# point A
A = [1 2]

%# neighboring points
k = 2;                               %# hop size
[sx,sy] = meshgrid(-k:k,-k:k);       %# steps to get to neighbors
xx = bsxfun(@plus, A(1), sx(:));     %# add shift in x-coords
xx = min(max(xx,1),sz(2));           %# clamp x-coordinates within range
yy = bsxfun(@plus, A(2), sy(:));
yy = min(max(yy,1),sz(1));
B = unique([xx yy],'rows');          %# remove duplicates
B(ismember(B,A,'rows'),:) = [];      %# remove point itself

The result for the point A = (1,2) with k=2 hops: A = (1,2)k=2跳的结果:

B =
     1     1
     1     3
     1     4
     2     1
     2     2
     2     3
     2     4
     3     1
     3     2
     3     3
     3     4

and an illustration of the solution: 以及解决方案的说明:

x A x x . .
x x x x . .
x x x x . .
. . . . . .
. . . . . .

lets say A = [Xcenter Ycenter] 可以说A = [Xcenter Ycenter]

for K-hop, you can access points: 对于K-hop,您可以访问以下点:

pointsX = [];
pointsY = [];
for i=-k:k
  pointsX = [pointsX  Xcenter+i]; 
  pointsY = [pointsY  Ycenter+i];
end

Furthermore, you can filter these points by order coordinates and remove the outliers. 此外,您可以按订单坐标过滤这些点并删除异常值。 eg consider 例如考虑

(1,1)  (1,2)  (1,3)
(2,1)  (2,2)  (2,3)
(3,1)  (3,2)  (3,3)

Now you know that minimum allowed X and Y are 1, so just filter out points with any ordinate and/or abscissa lesser than that. 现在您知道允许的X和Y的最小值为1,因此只需过滤出坐标和/或横坐标小于此值的点即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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