2012-01-03 25 views
1

私は角を(±1,±1)にして、2D直交座標系で正方形をプロットしたいと思います。私はそれを400個のより小さい正方形にさらに分割し、それぞれの辺の長さを0.1としたいと思います。MATLABで四角形のグリッドをプロットする

どうすればこのことができますか?

+0

これまでに何を試みましたか?何がうまくいかなかったのですか? –

答えて

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) 
関連する問題