如何在带有循环的matlab中制作网格

How to make a grid In matlab with loop

我想在没有 Meshgrid 的情况下在 matlab 中制作网格(统一映射)。

我已经用 meshgrid 制作了网格,但现在我只想用循环或任何第二种方法(没有 meshgrid)制作它

这是我使用 meshgrid 的代码:

  figure(6)
  [X,Y] = meshgrid(-1:0.1:1, -1:0.1:1)
  plot(X,Y,'k-')
  hold on
  plot(Y,X,'k-');

使用 repmat,或乘以 ones 向量以获得更基本的功能:

x = -1:0.1:1;
y = -1:0.1:1;
% with repmat
X1 = repmat(x(:)',[numel(y),1]);
Y1 = repmat(y(:),[1,numel(x)]);
% multiply with ones
X2 = ones(numel(y),1)*x(:)';
Y2 = y(:)*ones(1,numel(x));
% meshgrid
[X3,Y3] = meshgrid(x, y);
isequal(X1,X2,X3) && isequal(Y1,Y2,Y3) % true

plot(X1,Y1,'k');
hold on
plot(Y1,X1,'k');