MATLAB在XY平面中移动点 [英] MATLAB moving a point in the XY plane
问题描述
在MATLAB中,我有一个由有限数量的点表示的XY平面. x的可能值存储在向量X中,y的可能值存储在另一个向量Y中.我有一个点,说A,使得A(1)属于X的x坐标和A(2 )所属的Y是y坐标.
In MATLAB, I have an XY plane that is represented by finite number of points. 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.
如果A点位于中间,则它可以以8种方式之一移动:
This point A can move in one of 8 ways if it is in the middle:
. . . . A . .
. A . OR . . OR . A
. . . . .
当然,如果点A在边上(有时只有5个,如果是角则只有3个),这些点的集合也会改变.如何找到这些"1跳"相邻点的集合?那套"k-hop"相邻点呢?所谓集合,是指两个向量,一个用于x坐标,另一个用于y坐标.谢谢!
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). How can I find the set of these "1-hop" neighboring points? What about the set of "k-hop" neighboring points? By set I mean two vectors one for x-coordinates and another for y-coordinates. Thanks!
推荐答案
请考虑以下代码:
%# 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
具有k=2
跳的点A = (1,2)
的结果:
B =
1 1
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
和解决方案的说明:
x A x x . .
x x x x . .
x x x x . .
. . . . . .
. . . . . .
这篇关于MATLAB在XY平面中移动点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!