考虑两个维度(x,y)
。
我想在Matlab中构建点{@ 1}}和-0.8<x<0.1
的点网格。从这样的网格我想删除所有点0.1<y<0.9
和-0.7<=x<=0
。
我使用&#34;网格精度&#34;等于0.2<=y<=0.8
。
这里我的代码似乎没有做我想要的
0.001
%outside square
xgrid=(-0.8+0.001:0.001:0.1-0.001);
ygrid=(0.1+0.001:0.001:0.9-0.001);
[p,q] = meshgrid(xgrid, ygrid);
grid = [p(:) q(:)];
%inside square
xgrid1=(-0.7:0.001:0);
xgrid1=(0.2:0.001:0.8);
[p,q] = meshgrid(xgrid1, ygrid1);
grid1 = [p(:) q(:)];
%delete inside square from outside square
grid(ismember(grid,grid1, 'rows'),:)=[];
似乎无法识别Ismember
中的所有点都在grid1
中。我做错了什么?
答案 0 :(得分:1)
请参阅此代码,其中包含描述要执行操作的注释
% Define grid precision, h, for more clarity and easier editing
h = 1e-3;
% Specify xgrid and ygrid using 2 parts each
xgrid = [-0.8+h:h:-0.7-h, 0+h:h:0.1];
ygrid = [0.1+h:h:0.2-h, 0.8+h:h:0.9-h];
% Make grid and use it to get coordinate pairs
[p,q] = meshgrid(xgrid, ygrid);
grid = [p(:) q(:)];
您正在尝试创建一个大网格,然后从中删除一个内部方块。上面的代码只创建了生成的矩阵,从不首先创建内部正方形。
修改强>
OP澄清了只应删除-0.7<=x<=0.0
和0.2<=y<=0.8
的坐标,以上是针对“OR”的情况。以下是所谓的“AND”案例......
% Define grid precision, h, for more clarity and easier editing
h = 1e-3;
% Specify xgrid and ygrid using 2 parts each
xgrid = [-0.8+h:h:0.1];
ygrid = [0.1+h:h:0.9-h];
% Make grid and use it to get coordinate pairs
[xx, yy] = meshgrid(xgrid, ygrid);
% Turn grids into column vectors
xx = xx(:); yy = yy(:);
% Logical indexing for where range removal condition is met
idx = (xx >= -0.7 & xx <= 0.0) & (yy >= 0.2 & yy <= 0.8)
% grid is all values of xx and yy which are NOT true in idx, so ~idx
grid = [xx(~idx) yy(~idx)];