2012-01-03 55 views
1

我想在2D笛卡爾座標系中繪製一個正方形,其角落爲(±1,±1)。我想進一步將它分成400個更小和相等的正方形,每個邊長爲0.1在MATLAB中繪製一個正方形網格

如何在MATLAB中做到這一點?

+0

你到目前爲止嘗試了什麼?什麼沒有用? – 2012-01-03 12:08:41

回答

1

請參閱rectangle函數。例如,嘗試

% Draw large bounding box: 
xstart = -1; 
ystart = -1; 

xlen = 2; 
ylen = 2; 

rectangle('position', [xstart, ystart, xlen, ylen]) 

% Draw smaller boxes 
dx = 0.1; 
dy = 0.1; 

nx = floor(xlen/dx); 
ny = floor(ylen/dy); 

for i = 1:nx 
    x = xstart + (i-1)*dx; 
    for j = 1:ny 
     y = ystart + (j-1)*dy; 
     rectangle('position', [x, y, dx, dy]) 
    end 
end 
2

您可以用垂直線和水平線權數生成網格:

%% 
N = 400; 
x = linspace(-1,1,sqrt(N)+1) 
y = linspace(-1,1,sqrt(N)+1) 

% Horizontal grid 
for k = 1:length(y) 
    line([x(1) x(end)], [y(k) y(k)]) 
end 

% Vertical grid 
for k = 1:length(y) 
    line([x(k) x(k)], [y(1) y(end)]) 
end 

axis square 
2

這看起來像一個問題,我不得不解決的問題。我在下面做的是用meshgrid獲取所有點的座標。然後,我得到從everey點到pdist的每個其他點的距離,當距離爲1時,它是我們要繪製的連接。然後我們繪製所有這些線。

%# enter your prerequisites 
I=400; R=0.1; N=sqrt(I); M=sqrt(I); 

%# create vertices on square grid defined by two vectors 
[X Y] = meshgrid(1:N,1:M); X = X(:); Y = Y(:); 

%# create adjacencymatrix with connections between all neighboring vertices 
adjacency = squareform(pdist([X Y], 'cityblock') == 1); 

%# plot adjacenymatrix on grid with scale R and origin at the center 
[xx yy] = gplot(adjacency, [X Y]); 
xx = xx-round(sqrt(I)/2); %# this centers the origin 
yy = yy-round(sqrt(I)/2); 
plot(xx*R, yy*R)