Search code examples
matlabplotmatlab-figure

How to make a grid In matlab with loop


I want to make a grid (Uniform Mapping) in matlab without Meshgrid.

I have make grid with meshgrid but now I just want to make it with a loop or any Second Method (without meshgrid)

This is my code using meshgrid:

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

Solution

  • use repmat, or multiply by ones vectors for even more basic functionality:

    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');